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

前端 未结 4 703
星月不相逢
星月不相逢 2020-12-07 00:16

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

相关标签:
4条回答
  • 2020-12-07 00:59

    Just change the delimiters temporarily:

    {{=<% %>=}}
    {{Look at the curlies!}}
    <%={{ }}=%>
    
    0 讨论(0)
  • 2020-12-07 01:01

    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

    0 讨论(0)
  • 2020-12-07 01:04

    Just add one curly brace,

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

    0 讨论(0)
  • 2020-12-07 01:06

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

    0 讨论(0)
提交回复
热议问题