handlebars.js 帮助程序不起作用
我在这里有点失去理智......我有以下代码:
<script id="myTemplate" type="text/x-handlebars-template">
<div>{{post/name}}</div>
{{#if post/attachments}}
<p>
{{#list post/attachments}}<img src="{{url}}">{{/list}}
</p>
{{/if}}
</script>
以及一个我直接从每个助手复制的助手,以确保没有错误。
Handlebars.registerHelper('list', function(context, fn, inverse) {
var ret = "";
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
}
} else {
ret = inverse(this);
}
return ret;
});
window.myTemplate = Handlebars.compile($('#myTemplate').html());
window.myTemplate({post:{name:"Post!", attachments:[{url:"/images/preview.jpg"},{url:"/images/1.jpg"}]
这实际上并没有调用帮助器,代码最终看起来像这样:
<div></div>
<p>
<img src>
</p>
现在我用每个块执行此操作并且它工作正常,我缺少什么?
I am somewhat losing my mind here... I have the following code:
<script id="myTemplate" type="text/x-handlebars-template">
<div>{{post/name}}</div>
{{#if post/attachments}}
<p>
{{#list post/attachments}}<img src="{{url}}">{{/list}}
</p>
{{/if}}
</script>
And a helper which i copied directly from the each helper to make sure there were no errors.
Handlebars.registerHelper('list', function(context, fn, inverse) {
var ret = "";
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
}
} else {
ret = inverse(this);
}
return ret;
});
window.myTemplate = Handlebars.compile($('#myTemplate').html());
window.myTemplate({post:{name:"Post!", attachments:[{url:"/images/preview.jpg"},{url:"/images/1.jpg"}]
This does not actually call the helper, code ends up looking like this:
<div></div>
<p>
<img src>
</p>
Now I do this with an each block and it works fine, what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果我在我的页面上多次包含了handlebars.js,这导致我的助手被忽略。
现在,如果我能找到一种方法来恢复我生命中的那些时光就好了……
Turns out I was including handlebars.js more than once on my page, which was causing my helpers to be ignored.
Now, if only I could find a way to get those hours of my life back...