Counter for handlebars #each

后端 未结 8 948
小鲜肉
小鲜肉 2021-02-04 02:29

In Handlebars, say i have a collection of names how can i do

{{#each names}}
{{position}}
{{name}}
{{/each}}

where {{position}} is

8条回答
  •  太阳男子
    2021-02-04 02:53

    You can do this with the built-in Handlebars @index notation:

    {{#each array}}
      {{@index}}: {{this}}
    {{/each}}
    

    @index will give the (zero-based) index of each item in the given array.

    Please note for people using Handlebars with the Razor view engine you must use the notation @@index to avoid compilation errors.

    For more built-in helpers see http://handlebarsjs.com/

提交回复
热议问题