如何在Mustache模板中使用文字“{{”?
另外,如果我正在使用自定义标签,例如<%
和%>
,是否有方法编写"<%"?
从理论上讲,我可以使用不同的标签,但我已经编写了太多使用{{
和}}
的代码,无法将它们全部更改。
暂时更改分隔符:
{{=<% %>=}}
{{Look at the curlies!}}
<%={{ }}=%>
{{
,你应该写成{{
。
要输出<%
,你应该写成<%
。您可以很容易地单独使用{{
。如果您想记录像{{example}}
这样的内容,您可以始终使用数据传递前两个花括号。
orphaned curlies are easy {{ <br>
{{curly}}example}} curlies are harder
var data = { 'curly' : '{{'},
tpl = $('#curly').html(),
html = Mustache.to_html(tpl, data);
document.write(html);
导致结果:
orphaned curlies are easy {{
{{example}} curlies are harder
这是完整的可工作的jsFiddle:链接
const data = {"charsToBeEscaped": "<%"}
模板:a special char like that is perfectly rendered! {{{charsToBeEscaped}}}
,它会生成:is perfectly rendered! <%
。import Mustache from 'mustache';
const template = '{{ # literal }}{{Look at the curlies!}}{{ / literal }}';
const view = {
literal: () => text => text
};
const output = Mustache.render(template, view);
console.log(output); // {{Look at the curlies!}}
literal
部分中的任何内容,这可能是你使用场景中期望的,也可能不是。
}
表示}
。 - mems