2

Say I have the following code:

<div>
  {{#each questions}}
  <div id="question_{{@index}}">
    {{#each this.answers}}
    <div id="answer_{{???}}_{{@index}}">
      {{this}}
    </div>
    {{/each}}
  </div>
  {{/each}
</div>

How can I access the outer loop's index (question index) inside of the inner (answer) loop? Essentially I want the id in the format of "answer_questionIndex_answerIndex"

2
  • I was gonna say: start with this link to learn how to get index or key as required, and then combine that with this link Commented Dec 19, 2019 at 14:40
  • However, I no longer think that's possible, and you might have to do some data manipulation BEFORE you render your data -- put the index of the quesion inside the answers or the question, as a property Commented Dec 19, 2019 at 14:40

1 Answer 1

4

Found this deep in some documentation

Block Parameters

New in Handlebars 3.0, it's possible to receive named parameters from supporting helpers.

{{#each users as |user userId|}}
  Id: {{userId}} Name: {{user.name}}
{{/each}}

In this particular example, user will have the same value as the current context and userId will have the index value for the iteration.

https://handlebarsjs.com/guide/block-helpers.html#hash-arguments

Sign up to request clarification or add additional context in comments.

1 Comment

I spent way too much time browsing the documentations and googling and found nothing, but this is perfect! Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.