2015-09-12 64 views
0

我想在另一個裏面使用迭代助手。但內部幫手不輸出任何內容。Handlebars嵌套迭代內置助手 - 空輸出

這就是車把模板:

{{#people}} 
    <h4>{{firstName}} {{lastName}}</h4> 
    {{#students}} 
    <h3>{{firstName}} {{lastName}}</h3> 
    {{/students}} 
{{/people}} 

這是我的JSON:

{ 
    people : [ 
    { firstName: "Yehuda", lastName: "Katz" }, 
    { firstName: "Carl", lastName: "Lerche" }, 
    { firstName: "Alan", lastName: "Johnson" } 
    ], 
    students: [ 
    { firstName: "Mike", lastName: "Smith" }, 
    { firstName: "Dan", lastName: "Knight" }, 
    { firstName: "Griffin", lastName: "Smith" } 
    ] 
} 

請告訴我這個問題的解決方案? 嘗試代碼在線和快速瀏覽:http://tryhandlebarsjs.com/

回答

0

表達{{#people}} ... {{/people}}產生了三個背景:

{ firstName: "Yehuda", lastName: "Katz" }, 
{ firstName: "Carl", lastName: "Lerche" }, 
{ firstName: "Alan", lastName: "Johnson" } 

所以有上述任何上下文沒有學生,這就是爲什麼內表達式{{#students}} ... {{/students}}不給出輸出。

你能詳細說明你試圖實現什麼嗎?

+0

喜IM在店項目。我有3類男士服裝。 即時通訊嘗試爲每個類別添加一個div中的子類別。 上面的例子如果和我的項目一樣。 –

0

非常感謝ĴP ....你的答案是偉大的... 正確的代碼是:

{ 
    people : [ 
    { firstName: "Yehuda", lastName: "Katz", students: [{ firstName1: "Mike", lastName1: "Smith" 
}] }, 
    { firstName: "Carl", lastName: "Lerche", students: [{ firstName1: "Mike", lastName1: "Smith" 
}] }, 
    { firstName: "Alan", lastName: "Johnson", students: [{ firstName1: "Mike", lastName1: "Smith" 
}] } 
    ], 
}