In meteor is there a way to access array index in spacebars [duplicate]

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>

Leave a Comment