Reputation: 2894
With CakePHP, it's possible to insert normal JavaScript by putting the following inside a view:
$this->Html->scriptStart(array('inline' => false));
echo 'alert("Hello world!");';
$this->Html->scriptEnd();
Unfortunately, if you have multiple echo statements, all the text is squashed into 1 long line. Is there any way to split up the lines and insert newlines?
I've already tried adding \n to the end of the echo'd statement to no avail.
I think it's possible to have JS run correctly all in 1 line with proper use of semicolons but it makes for a painful reading and debugging experience.
Upvotes: 1
Views: 2362
Reputation: 2894
I ended up solving the problem by using Js->buffer as follows:
echo $this->Js->buffer('$(\'<input type="button" id="instaSave" value="Save"/>\')
.click(function(){
$(this).val("Saving...");
$(this).parents("form:first").ajaxSubmit({
success: function(responseText, responseCode) {
$(".food").each(function(fIndex){
...
');
Upvotes: 0
Reputation: 409
You could try not putting it in PHP if you dont need to:
<?php $this->Html->scriptStart(array('inline' => false)); ?>
alert("Hello world!");
alert("Hello world!");
<?php $this->Html->scriptEnd(); ?>
Then just use normal js formatting.
Or, you can use PHP_EOL
to echo a newline that isn't within a string literal.
<?php
$this->Html->scriptStart(array('inline' => false));
echo 'alert("Hello world!");' . PHP_EOL;
echo 'alert("Hello world!");';
$this->Html->scriptEnd();
?>
Upvotes: 2
Reputation: 14827
I'm not sure I'm understand your question correctly but try to escape your \n
since php will interpret it as a normal string before your javascript take turn, so for example:
$first = "This is first line\\n";
$both = $first . "This is second line";
Upvotes: 0