![](/img/trans.png)
[英]How do I use nested iterators with Mustache.js or Handlebars.js?
[英]How to override the default output interpolation of Mustache.js, Hogan.js or Handlebars.js?
有没有办法更改Mustache,Hogan和Handlebars等库的默认输出?
例如,这是我的上下文:
var context = {
primitive: 1,
array: [1, 2, 3],
object: new Object()
}
那就是我希望编译时的结果如何:
<div>
Primitive: {{primitive}}
Array: {{array}} should output <Array#id> instead of "1,2,3"
Object: {{object}} should output <Object#id> instead of "[Object object]"
</div>
我知道我可以覆盖这些属性的toString方法,但是在插值发生之前是否还有其他类似回调的方法?
和Hogan在一起,取决于您真正想要做什么。 https://github.com/twitter/hogan.js#features
var text = "{{^check}}{{#i18n}}No{{/i18n}}{{/check}}";
text += "{{#check}}{{#i18n}}Yes{{/i18n}}{{/check}}";
var tree = Hogan.parse(Hogan.scan(text));
// outputs "# check"
console.log(tree[0].tag + " " + tree[0].name);
// outputs "Yes"
console.log(tree[1].nodes[0].nodes[0]);
标记被解析出来,您可以根据需要操作树,然后在完成后对其进行编译
刚刚发现,您可以重写Handlebars.Utils.escapeExpression
以在模板内部实现此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.