Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dust.js escape characters

I am trying to print a newline and per the Dust.js guide, in the template I have a {~n} at the end of each record output

{#friends} {name}, {age}{~n}{/friends}

I compiled the above template and am rendering with a couple of JSON records to generate 'out' object. I am using the document.getElementById('divID').innerHTML to replace the 'out' object dynamically in a basic DIV tag. The problem is am not able to print newline inspite of the {~n}. I have also tried {~r}, but still not luck. Any thoughts?

like image 848
Noosphere Avatar asked Mar 09 '26 19:03

Noosphere


2 Answers

HTML collapses adjacent whitespace (including newline characters) into a single space by default. There is no difference between a word and a \n word (or a \r word or a \r\n word for that matter) by default for most elements. The solution is either to:

  • Use a <br> element to add the break
  • Set the style on your div to be whitespace: pre;

Alternately, you can use a list (which is probably more semantic for your use case and should be preferred).

like image 118
Sean Vieira Avatar answered Mar 11 '26 09:03

Sean Vieira


If you're rendering as HTML, you need to use a <br> tag instead of a newline.

like image 37
lunixbochs Avatar answered Mar 11 '26 08:03

lunixbochs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!