无法理解如何转义站在车把 java 模板引擎的表达式旁边的 { 或 } 符号。
我正在使用车把模板来生成纯文本,因此我不能按照那里的建议使用大括号的 HTML ASCII 代码。
我需要表达喜欢\{{{variable.name}}\}
被解决{variable.value}
。我应该为此创建助手还是有更清洁的方法?
无法理解如何转义站在车把 java 模板引擎的表达式旁边的 { 或 } 符号。
我正在使用车把模板来生成纯文本,因此我不能按照那里的建议使用大括号的 HTML ASCII 代码。
我需要表达喜欢\{{{variable.name}}\}
被解决{variable.value}
。我应该为此创建助手还是有更清洁的方法?
下面是一些逃避的例子。最后一个方法使用助手转义(当其他方法不可用时)。
$(document).ready(function () {
var context = {
"textexample1" : "hello",
"textexample2" : "<div><b>hello</b></div>",
"textexample3" : "{ 'json' : 'sample }"
};
Handlebars.registerHelper('surroundWithCurlyBraces', function(text) {
var result = '{' + text + '}';
return new Handlebars.SafeString(result);
});
var source = $("#sourceTemplate").html();
var template = Handlebars.compile(source);
var html = template(context);
$("#resultPlaceholder").html(html);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="sourceTemplate" type="text/x-handlebars-template">
Simple text : {{textexample1}}<br/>
Html text not escaped : {{textexample2}}<br/>
Html text escaped : {{{textexample2}}}<br/>
Json text : {{textexample3}}<br/>
Non JSON text with surrounding mustaches {} : { {{textexample1}} }<br/>
Non JSON text with surrounding mustaches (no space) : {{{textexample1}}}<br/>
Solution with helper : {{#surroundWithCurlyBraces textexample1}}{{/surroundWithCurlyBraces}}
</script>
<br/>
<div id="resultPlaceholder">
</div>
{{myvar}}{{append '' '}'}}
从这里附加助手: https ://www.npmjs.com/package/handlebars-helpers#string