user583726
user583726

Reputation: 685

How to get index for parent loop in Handlebars template?

<script id="contact-row" type="text/x-handlebars-template" >

    {{#each rows}}
      <tr>
           {{getInputField @index "country" country }}

          {{#each contactData}}
            {{getInputFieldForData @index "contractName" contractName }}
          {{/each}}
      </tr>
    {{/each}}

I want to get the index of the parent in the inner #each loop. I tried ../@index but that gives error.

Upvotes: 1

Views: 3113

Answers (2)

datUser
datUser

Reputation: 293

Looks like things have changed...

To get the index of a parent {{#each}} block from within a child {{#each}} block use the {{@../index}} syntax.

{{#each foo}}
    {{@index}}           // Parent Index Reference
    {{someProperty}}     // Parent property
    {{#each baz}}
        {{@index}}       // Child Index Reference
        {{@../index}}    // Parent Index Reference <--
        {{someProperty}} // Child property
    {{/each}}
{{/each}}

The link in the accepted answer has this solution, just posting the details here for posterity's sake.

Upvotes: 3

Cole Pilegard
Cole Pilegard

Reputation: 582

It looks like this is not currently possible the way you want to do it: https://github.com/wycats/handlebars.js/issues/491

But you could set the index to a new variable in the outer scope to access it.

Upvotes: 2

Related Questions