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

可紊 提交于 2019-11-26 22:00:32

问题


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

On a side note, if I'm using custom tags, like <% and %>, is there a way to write "<%"?

Theoretically, I could use different tags, but I have too much code written using {{ and }} to change it all.


回答1:


Just change the delimiters temporarily:

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



回答2:


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;%.




回答3:


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




回答4:


Just add one curly brace,

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



来源:https://stackoverflow.com/questions/11042926/how-does-one-use-a-literal-in-a-mustache-template

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