Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

javascript - 如何在Handlebars中获得每个助手的索引?(How to get index in Handlebars each helper?)

I'm using Handlebars for templating in my project.(我在我的项目中使用Handlebars进行模板化。)

Is there a way to get the index of the current iteration of an "each" helper in Handlebars?(有没有办法获得Handlebars中“每个”助手的当前迭代的索引?)
<tbody>
     {{#each item}}
         <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
            <td>{{this.key}}</td>
            <td>{{this.value}}</td>
         </tr>
     {{/each}}
</tbody>
  ask by thunderboltz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the newer versions of Handlebars index (or key in the case of object iteration) is provided by default with the standard each helper.(在较新版本的Handlebars索引(或对象迭代的情况下的键)默认提供标准的每个帮助程序。)


snippet from : https://github.com/wycats/handlebars.js/issues/250#issuecomment-9514811(摘录自: https//github.com/wycats/handlebars.js/issues/250#issuecomment-9514811)

The index of the current array item has been available for some time now via @index:(现在通过@index可以获得当前数组项的索引一段时间了:)

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

For object iteration, use @key instead:(对于对象迭代,请使用@key代替:)

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...