handlebars-lang / handlebars-lang/handlebars.js
Unable to access #each helper variables in child scope
- Dominant language
- JavaScript
- Stars
- 18.7k
- Forks
- 2.1k
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
Research direction
Start with the nested #each template and sample data in the issue, then reproduce the parse error using the linked tryhandlebarsjs example. Done means determining whether parent #each data variables should be accessible through ../@index and adding the corresponding behavior or documentation clarification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100