1

I would like to know how to pass the new @index variable to a child template in Meteor 1.2. I've got something like:

    {{#each scores}}
          {{@index}} //<---- THIS WORK
        {{> scoreItem}}
    {{/each}}

    <template name="scoreItem">
            <div class="position">
                {{@index}}. // <----- GIVE ERROR
            </div>
    </template>

I get always undefined or an error

LearningProcess
  • 607
  • 1
  • 8
  • 29

1 Answers1

2

For purposes of demonstration, I'm going to assume a score has a value property.

Option 1: pass index as part of the context

<template name="myTemplate">
  {{#each scores}}
    {{> scoreItem score=this index=@index}}
  {{/each}}
</template>

<template name="scoreItem">
  <div>{{score.value}} {{index}}</div>
</template>

Option 2: extend the context with index

This solution is similar to my answer to this question:

Template.registerHelper('extendContext', function(key, value) {
  var result = _.clone(this);
  result[key] = value;
  return result;
});
<template name="myTemplate">
  {{#each scores}}
    {{> scoreItem extendContext 'index' @index}}
  {{/each}}
</template>

<template name="scoreItem">
  <div>{{value}} {{index}}</div>
</template>
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Thank you for this answer. I thought there was a more direct way of accessing the index inside a template - but your two methods seem to be the only ones that work – Andy59469 Jun 13 '16 at 13:16