Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  25] [ 4]  / answers: 1 / hits: 18479  / 10 Years ago, sun, february 16, 2014, 12:00:00

I am using meteor Shark branch.



Is there a way to access array index inside each block helper in spacebars?



I am looking for something like this.



{{#each humans}}
{{this.arrayIndex}}
{{/each}}

More From » meteor

 Answers
10

meteor >= 1.2



Spacebars gained a lot of functionality in 1.2, including a native @index. Helpers are no longer needed to solve this problem - you can simply do this:



<template name=showHumans>
<ul>
{{#each humans}}
<li>{{@index}}: {{name}}</li>
{{/each}}
</ul>
</template>


meteor < 1.2



I saw a similar example using template helpers in the meteor book in the animations chapter. You can apply a map to the humans cursor in order to add an index like so:



Template.showHumans.helpers({
humans: function() {
return Humans.find({}, {sort: {hotness: -1}}).map(function(human, index) {
human.rank = index;
return human;
});
}
});


<template name=showHumans>
<ul>
{{#each humans}}
<li>{{rank}}: {{name}}</li>
{{/each}}
</ul>
</template>

[#72497] Friday, February 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;