8

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.

zessx
  • 68,042
  • 28
  • 135
  • 158
JavaUser
  • 25,542
  • 46
  • 113
  • 139

4 Answers4

13

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

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
5

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.

Community
  • 1
  • 1
str
  • 42,689
  • 17
  • 109
  • 127
5

white-space: pre-wrap, NOT white-space: pre (won't transfer a long string otherwise)

Naskalin
  • 895
  • 9
  • 9
-1

New lines characters are not rendered in HTML, you ma use <br> instead:

var test ="test1"+"<br>"+"test2"+"<br>"+"test3"
var test ="test1<br>test2<br>test3"
zessx
  • 68,042
  • 28
  • 135
  • 158