Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle new line in handlebar.js

I am using HandleBar.js in my rails jquery mobile application.

I have a json returned value data= "hi\n\n\n\n\nb\n\n\n\nhow r u"

which when used in .hbs file as {{data}} showing me as hi how r u and not as with the actual new line inserted

Please suggest me.

Pre tag helps me

like image 330
useranon Avatar asked Oct 08 '22 10:10

useranon


2 Answers

Handlebars doesn't mess with newlines in your data unless you have registered a helper which is doing something with them. A good way of dealing with newlines in HTML without converting them to br tags would be to use the CSS property white-space while rendering the handlebars template in HTML. You can set its value to pre-line.

Read the related documentation on MDN

like image 166
Jai Pandya Avatar answered Oct 13 '22 10:10

Jai Pandya


Look at the source of the generated file - your newline characters are probably there, HTML simply does not render newline characters as new lines.

You can insert a linebreak with <br />

However, it looks like you're trying to format the position of your lines using newline characters, which technically should be done by wrapping your lines in <p> or <div> tags and styling with CSS.

like image 39
Nathan Fig Avatar answered Oct 13 '22 11:10

Nathan Fig