[英]handlebars.js helper doesnt work
我在這里有些不知所措...我有以下代碼:
<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>
現在,我對每個塊都執行此操作,並且效果很好,我還缺少什么?
原來,我在頁面上多次包含handlebars.js,這導致我的助手被忽略。
現在,只要我能找到一種方法來恢復我的生命……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.