简体   繁体   English

小胡子js断线

[英]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 我将它传递给Mustache进行渲染,并期望它应该像打印一样

test1
test2
test3

But the actual result is test1test2test3 while rendering this. 但实际结果是test1test2test3渲染时。

Thanks. 谢谢。

你应该使用<br>但是在你的小胡子模板中你必须使用{{{myString}}}来逃避html

The best approach, in my opinion, is to apply the following CSS to the element you want to show with line breaks: 在我看来,最好的方法是将以下CSS应用于要使用换行符显示的元素:

white-space: pre;

That way, all the \\n will actually show as line breaks. 这样,所有\\n实际上都会显示为换行符。 There are also other ways of handling this, for example see Mustache.js allow only line breaks, escape other HTML . 还有其他处理方法,例如,请参阅Mustache.js仅允许换行,转义其他HTML

white-space: pre-wrap ,not white-space: pre (否则不传输长字符串)

New lines characters are not rendered in HTML, you ma use <br> instead: 新行字符不会在HTML中呈现,您可以使用<br>代替:

var test ="test1"+"<br>"+"test2"+"<br>"+"test3"
var test ="test1<br>test2<br>test3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM