逃避句柄中表达式旁边的花括号 [英] Escaping curly brackets standing next to expression in handlebars
本文介绍了逃避句柄中表达式旁边的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法理解如何在 handlebars Java模板引擎.
我正在使用车把模板生成纯文本,因此我不能按照建议使用大括号的HTML ASCII代码
I'm using handlebars templates to generate plain text so I can't use HTML ASCII codes of braces as advised there.
我需要像\{{{variable.name}}\}
这样的表达式才能解析为{variable.value}
.我应该为此创建助手吗?还是有更清洁的方法?
I need expression like \{{{variable.name}}\}
to be resolved to {variable.value}
. Should I create helpers for that or there is a cleaner way?
推荐答案
以下是转义的一些示例.最后一种方法是使用助手进行转义(其他方法无法使用时).
Here are some examples of escaping. The last method escape with a helper (when the other methods are not possible).
$(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>
这篇关于逃避句柄中表达式旁边的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文