handlebars.js icon indicating copy to clipboard operation
handlebars.js copied to clipboard

Unable to access #each helper variables in child scope

Open steveklett opened this issue 4 years ago • 5 comments

I can't find anything in the documentation that says I should be able to use ../@last but I also don't understand why I can't, or why it throws an error. The documentation states:

Some helpers like #with and #each allow you to dive into nested objects. When you include ../ segments in your path, Handlebars will change back into the parent context.

Template:

{{#each outer}}
{{#each inner}}
outer index: {{{../@index}}}
inner index: {{{@index}}}
{{/each}}
{{/each}}

with data:

{
  outer: [{
    inner: [{},{},{}]
  },{
    inner: [{},{},{}]
  }]
}

Throws error (using http://tryhandlebarsjs.com):

Error(s): Error: Parse error on line 3: ...outer index: {{{../@index}}}inner inde ----------------------^ Expecting 'ID', got 'DATA'

There is a specific scenario I have where I can't find a way to implement the use case without being able to access the #each helper variables from a child scope: https://stackoverflow.com/questions/67994959/how-to-avoid-trailing-comma-when-defining-json-array-by-nested-enumeration-of-hi

-Steve

steveklett avatar Jun 17 '21 03:06 steveklett

Figured it out: {{{@../index}}} Not sure if I should delete my issue or leave it so others benefit maybe?

steveklett avatar Jun 17 '21 04:06 steveklett

@steveklett If you have time, it would be nice if you could add documentation for this in https://github.com/handlebars-lang/docs.

jaylinski avatar Nov 23 '21 23:11 jaylinski

I am facing an error while trying to run doc repo locally(SSL error). Refer to the discussion: https://github.com/handlebars-lang/handlebars.js/discussions/1944

satyajitnayk avatar Jun 02 '23 03:06 satyajitnayk

@steveklett If you have time, it would be nice if you could add documentation for this in https://github.com/handlebars-lang/docs.

@jaylinski shall I add documentation for it?

satyajitnayk avatar Jun 13 '23 05:06 satyajitnayk

@steveklett If you have time, it would be nice if you could add documentation for this in https://github.com/handlebars-lang/docs.

@jaylinski shall I add documentation for it?

@satyajitnayk Yes, that would be nice.

jaylinski avatar Jun 13 '23 12:06 jaylinski