我有一个不寻常的情况,客户端想要在从 JSON 对象传递到 Handlebars 的字符串中放置一个换行符。我试过转义字符,但不出所料,它不会由 DOM 呈现。有什么建议吗?
"company": "Lorem adscs ireland <br/> marketed as iuhmdsf in Europe"
var products = Data;
var theTemplateScript = $("#product-template").html();
var theTemplate = Handlebars.compile (theTemplateScript);
$("#marketed-products .products").append (theTemplate(products));
{{#items}}
<li><span class="company">{{company}}</li>
{{/items}}
上面代码的输出应该是这样的
Lorem adscs 爱尔兰
在欧洲作为 iuhmdsf 销售
最佳答案
使用三括号,如 {{{returnedHtml}}}
,在这种情况下,Handlebars 不会对值进行转义。
即它将变成:
{{#items}}
<li><span class="company">{{{company}}}</li>
{{/items}}
关于javascript - 将 HTML 标记传递到 Handlebars 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505944/