5

I have a 2D array in a JSON object (called table ;)

data = {

tableID : "testTable",

table : [
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}]
]

};

And have been successfully rendering it out with handlebars using the template:

<table id = "{{tableID}}-table">

{{#each table}}

    <tr id = "{{../tableID}}-row-{{@index}}">

        {{#each this}}

            <td id = "{{../../tableID}}-row-{{../index}}-col-{{@index}}">

                {{this.type}}-{{this.value}}

            </td>

        {{/each}}

    </tr>

{{/each}}

</table>

However, in the td tag id I can't seem to access the parent index {{../index}} - the index of the row. Nothing is returned:

<td id = "testTable-row--col-x">

However, I can access the index of the current context {{@index}}.

Any ideas??

Many many thanks in advance!!

Rich

p.s. Using 1.0.0-rc.3

2
  • Yes, that's a good point. It's the direction I've adopted at the moment, but was hoping for something a bit cleaner ;) Commented Apr 2, 2013 at 10:46
  • possible duplicate of Handlebars.js: How to access parent index in nested each? Commented Nov 11, 2014 at 17:54

2 Answers 2

3

Since Handlebars version 2.0.0, you can use

{{@../index}}
Sign up to request clarification or add additional context in comments.

Comments

2

This is an open issue/feature on handlebar. You can check the progress for the same here

However you can check the workaround here

Comments

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.