2015-02-17 27 views
1

我傳遞給我的模板。如何訪問Handlebars模板中的對象鍵?

modules: { 
    left: [], 
    center: ['card', 'progressChart', 'tableOfChildren'], 
    right: ['activity', 'details', 'triggers'] 
} 

我想要做這樣的事情......(僞,我不認爲會工作代碼)

{{#each region in modules}} 
    <div class="{{region}}"> 
    {{#each region}} 
    <div class="{{this}} module"></div> 
    {{/each}} 
    </div> 
{{/each}} 

回答

1

可能duplicate,其中Jon提供了最好的解決方案於:

對於數組:

{{#each myArray}} 
    Index: {{@index}} Value = {{this}} 
{{/each}} 

對於對象:

{{#each myObject}} 
    Key: {{@key}} Value = {{this}} 
{{/each}} 

注意,只透過hasOwnProperty測試屬性將被枚舉。