Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache js line break

I am passing a text with line break to mustache but while rendering the line breaks are not shown. Please let me know , how to add line breaks in the text.

var test ="test1"+"\n"+"test2"+"\n"+"test3".

I am passing this to Mustache for rendering and expecting this should get printed like

test1
test2
test3

But the actual result is test1test2test3 while rendering this.

Thanks.

like image 242
JavaUser Avatar asked Apr 20 '16 07:04

JavaUser


2 Answers

You should use <br> but in your mustache template you must use {{{myString}}} to escape html

like image 105
Quentin Roger Avatar answered Nov 08 '22 20:11

Quentin Roger


The best approach, in my opinion, is to apply the following CSS to the element you want to show with line breaks:

white-space: pre;

That way, all the \n will actually show as line breaks. There are also other ways of handling this, for example see Mustache.js allow only line breaks, escape other HTML.

like image 39
str Avatar answered Nov 08 '22 21:11

str