Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handlebars.js “each” loop inside another “each” loop 3

Suppose I want to build a dynamic table. How do I run each inside each. If the only varible that represents current item is this.

   {{#each by_width}}
       {{#each by_height}}
          {{this}} // how do refer to this from the outer loop?
       {{/each}}
   {{/each}}
like image 380
Pol Avatar asked Dec 27 '12 18:12

Pol


1 Answers

You can use ../ to access the parent in a Handlebars template:

{{#each by_width}}
    {{#each by_height}}
       w: {{../this}}
       h: {{this}}
    {{/each}}
{{/each}}

That of course assumes that by_height is inside each element of by_width, if they're both at the top level then you'd need another ../:

{{#each by_width}}
    {{#each ../by_height}}
       w: {{../this}}
       h: {{this}}
    {{/each}}
{{/each}}

Demo: http://jsfiddle.net/ambiguous/PNTXw/

like image 60
mu is too short Avatar answered Nov 02 '22 10:11

mu is too short