How does one use a literal {{ in a Mustache template?

依然范特西╮ 提交于 2019-11-28 01:48:28

Just change the delimiters temporarily:

{{=<% %>=}}
{{Look at the curlies!}}
<%={{ }}=%>

Assuming you are outputting HTML you could use an HTML entity to avoid it (mustache doesn't have any way to escape the opening tag built in).

So to output {{ you would write &#123;{.
To output <% you would write &lt;%.

You can use {{ by itself quite easily. If you are trying to document something like {{example}} you could always pass in the first two cur lies with your data.

orphaned curlies are easy {{ <br>
{{curly}}example}} curlies are harder

Some simple rendering:

var data = { 'curly' : '{{'},
    tpl = $('#curly').html(),
    html = Mustache.to_html(tpl, data);

document.write(html);​

Results in:

orphaned curlies are easy {{ 
{{example}} curlies are harder

Here's the full working jsFiddle

Just add one curly brace,

someTest = "<example>", {{someTest}} -> "&lt;example&gt;" {{{someTest}}} -> "<example>"

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