5

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

I am looking for something like this.

{{#each myarray}}
    {{this.arrayIndex3}}
{{/each}}
nilsi
  • 10,351
  • 10
  • 67
  • 79
james
  • 169
  • 1
  • 10

1 Answers1

1

I'm afraid there is not yet a standard way to do this, however you can write a helper that maps your array to a list of index / value pairs and iterate over it to display what you want.

JS

Template.myTemplate.helpers({
  myArrayWithIndex: function(){
    return _.map(this.myArray,function(value,index){
      return {
        index:index,
        value:value
      };
    });
  }
});

HTML

<template name="myTemplate">
  {{#each myArrayWithIndex}}
    myArray[{{index}}] == {{value}}
  {{/each}}
</template>

You could also define your own block helper called {{#eachWithIndex}} that would automate this process.

saimeunt
  • 22,666
  • 2
  • 56
  • 61