2015-02-17 28 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测试属性将被枚举。