Insert html in a handlebar template without escaping

徘徊边缘 提交于 2019-11-26 10:25:49

问题


Is there a way to insert a string with html tags into a handlebars template without getting the tags escaped in the outcoming string?

template.js:

<p>{{content}}</p>

use the template

HBS.template({content: \"<i>test</i> 123\"})

actual outcome:

<p>&lt;i&gt;test&lt;/i&gt; 123</p>

expected result:

<p><i>test</i> 123</p>

回答1:


Try like

<p>{{{content}}}</p>

I got the official reference to support my answer:

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{.




回答2:


In your template you must add triple mustaches like this. <p>{{{content}}}</p>




回答3:


According to Handlebars documentation, http://handlebarsjs.com/expressions.html

Quote from documentation,

If you don't want Handlebars to escape a value, use the "triple-stash", {{{

Pass the raw HTML to Handlebars template and get the raw HTML output by using triple brackets.

{{{foo}}}


来源:https://stackoverflow.com/questions/20280601/insert-html-in-a-handlebar-template-without-escaping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!