Handlebars.js: nested iteration with ability to access parent

Created on 18 Oct 2011  ·  5Comments  ·  Source: handlebars-lang/handlebars.js

Most helpful comment

{{#each families}}
  {{#each members}}
    <p>{{ ../surname }}</p>
    <p>{{ name }}</p>
  {{/each}}
{{/each}}

All 5 comments

One simple approach would be to register a helper:

{{#each animals}}
  {{#each organs}}
    {{get .. this}}
  {{/each}}
{{/each}}
Handlebars.registerHelper('get', function(obj, prop) {
  return obj[prop];
});

Sorry to bring this issue back up, but I don't see how this 'get' helper will give access to the parent unless we create circular references.

Could you expand on what you mean by "{{get .. this}}"? If this is an organ, how can we use that to find the parent animal?

I would like to use handlebars.js to iterate over a list of families, and then iterate over that family's members. Inside of both loops, I want to display properties of both. However, once I get into the second iteration, none of the family variables are visible.

{{#each families}}
  {{#each members}}
    <p>{{ ( here I want a family name property ) }}</p>
    <p>{{ ( here I want a member name property ) }}</p>
  {{/each}}
{{/each}}

Is this possible with handlebars? If not, that's OK. I'd greatly appreciate any help!

{{#each families}}
  {{#each members}}
    <p>{{ ../surname }}</p>
    <p>{{ name }}</p>
  {{/each}}
{{/each}}

hello guys i am learning node/express right now, the code above wont work for me i am using express-handlebars, can you help me? Is this possible with express-handlebars? here's my code. the gpareaData is iterating but the branch is not

{{#each gpareaData}}
  <tr>
    <td>
       <select class="form-control" name="gp_branch[]">
           <option value="{{id}}">{{desc}}</option>
             {{#each branch}}
           <option value="{{id}}">{{branch_desc}}</option>
             {{/each}}
       </select>        
     </td>
    <td><button type="button" class="btn btn-primary btn-sm" id="addRws3"><i class="fa fa-plus"></i></button></td>
</tr>
{{/each}}

{{#allData}}
all

{{#assignedData}}
assigned


{{#ifEqual id course_id }}
{{title}}

{{else}}
{{title}}

{{/ifEqual}}

{{/assignedData}}
{{/allData}}
My app control is nor even entering the inner loop . Just printing
all
all
all
Can you tell me why?

Was this page helpful?
0 / 5 - 0 ratings