Search code examples
javascripthandlebars.js

Handlebars.js: How to access parent index in nested each?


How to access parent @index value in each-loop?

Tried the following:

{{#each company}}
{{#each employee}}
  {{../@index}} // how to access company index here?
{{/each}}
{{/each}}

This results to an error:

Expecting 'ID', got 'DATA'


Solution

  • There is a syntax error in the example. The correct syntax is {{@../index}}.

    We are looking at ways that we can support custom naming of these parameters in future versions of the language so this is easier to deal with. https://github.com/wycats/handlebars.js/issues/907