2011-05-20 43 views
0

我有點在這裏失去我的心......我有以下代碼: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> 

現在我這樣做是與每個塊和它工作得很好,我缺少什麼?

回答

6

原來我在頁面上多次包含handlebars.js,這導致我的幫助器被忽略。

現在,如果只有我能找到一種方法讓我的生活中的那些小時回來......

+0

哇!我正在做同樣的事情。顯然這很容易做到! – pedalpete 2012-01-31 17:47:20