handlebars: how to access an array?

只愿长相守 提交于 2020-01-03 15:33:25

问题


I have the following simplified document:

{
    channel:'Channelname',
    users: [
        {userId:1},
        {userId:2},
        {userId:3}
    ]
}

How can i access the userId's in a {{#each}} loop like so:

{{#each channels}}
    {{channel}}
    {{#each channels.users}}
        {{userId}} //or {{channels.users.userId}} ?
    {{/each}}
{{/each}}

The first {{#each}} loop prints my channelname as expected, but the second {{#each}} loop doesn't print anything.

Regards, Cid


回答1:


Use

{{#each channels}}
    {{channel}}
    {{#each users}}
        {{userId}}
    {{/each}}
{{/each}}

When going into an each loop, handlebars will use the key names in the array directly.



来源:https://stackoverflow.com/questions/14758793/handlebars-how-to-access-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!