0

I use JavaScript-code in a velocity-template and it's not working!

I read content with this template and want to set this to a js-variable, but there are line-breaks in the content and I get the following error:

SyntaxError: unterminated string literal

In the rendered code there you see the error:

var exampleText = 'This is the first line

and this is the second line.';

In the original code it is written this way:

    var exampleText = '$question.answer.data';
var regularPanels = new A.Panel( { 
    bodyContent: exampleText, 
    collapsible: true, 
    collapsed: true,
    headerContent: '$question.data' } ) .render('#regularPanels$counter$reserved-article-id.data$randomNamespace');  

});

Is there a possibility to ignore the linebreak for the js-compilation, but still show it on the complete rendered page?


Okay, I solved it with the help of the EscapeTool by Velocity.

Combined with the answer from emiliocai it's the following code which works fine:

<div id="example-text" style="display:none;">
   <p>$escapeTool.java($question.answer.data).replace("\n","<br />")</p>
</div>

<script type="text/javascript" charset="utf-8">

AUI().ready('aui-panel', function(A) { 

var exampleText = document.getElementById('example-text').innerHTML;
var regularPanels = new A.Panel( { 
    bodyContent: exampleText, 
    collapsible: true, 
    collapsed: true,
    headerContent: '$question.data' } ) .render('#regularPanels$counter$reserved-article-id.data$randomNamespace');  

});

</script>

It might be, that it would work without the hidden <div>-Tag, but I haven't tested it yet.

So also possible would be:

var exampleText = '$escapeTool.java($question.answer.data).replace("\n","<br />")';

Tested it -> works!

  • not sure if I understand but can't you use
    or \n instead of newlines?
    – Emilio Rodriguez Feb 06 '14 at 09:40
  • Thats the problem. I would like so, but these newlines are generated by the template. In the database the content (question.answer.data) is saved with

    - tags for the new lines. That's okay, but I think they will be compiled to new lines before the content gets in the script-function. I tried to catch and replace the linebreaks on many ways but can't find a solution.
    – user3278740 Feb 06 '14 at 09:55

1 Answers1

0

I'm sorry but newlines are not acceptable in JavaScript, not sure how your template looks like but if you really cannot replace the newlines by \n or <br> the you can do a trick:

  • In the template render the contents coming from the database into a hidden div:

    <div id="example-text" style="display:none">$question.answer.data</div>

  • In your javascript code read the contents of the div into your variable:

    var exampleText = document.getElementById('example-text').innerHTML;

Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
  • thanks :) I already knew that newlines aren't acceptable, but it seems that velocity doesn't know :'D there is no error anymore and the content is shown, but now all is shown in one line. Isn't there any possibility to escape or catch a linebreak? – user3278740 Feb 06 '14 at 10:28
  • you could try replacing the \n for `
    ` like this: `var exampleText = document.getElementById('example-text').innerHTML.replace("\n","
    ");`
    – Emilio Rodriguez Feb 06 '14 at 10:39
  • I've already tried replacing, but it's still not working. It would be much easier if I would know what kind of linebreak is used by velocity, because it's neither \n nor

    (which is stored in the database).

    – user3278740 Feb 06 '14 at 10:58