问题
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 {{
.
To output <%
you would write <%
.
回答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}} -> "<example>"
{{{someTest}}} -> "<example>"
来源:https://stackoverflow.com/questions/11042926/how-does-one-use-a-literal-in-a-mustache-template