Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can handlebars walk up while iterating through lower level items?

With the latest stable version of Handlebar, I'm trying to display the site name while iterating through each children. The output of the code below is:

5
Stackoverflow
One -
Two -

Can handlebars retrieve the site name by walking up while rendering the children?

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <script src="handlebars.js"></script>
    <script>
    $(document).ready(function() {
        var source = $('#entry-template').html();
        var template = Handlebars.compile(source);
        var context = {
          site: {
            age: 5,
            name: 'Stackoverflow',
            children: [
              {
                name: 'One'
              },
              {
                name: 'Two'
              }
            ]
          }
        };
        $('div#output').html(template(context));
    });
    </script>
  </head>
  <body>
    <div id="output"></div>
    <script id="entry-template" type="text/x-handlebars-template">
      <div>{{site.age}}</div>
      <div>{{site.name}}</div>
      {{#each site.children}}
      <div>{{name}} - {{site.name}}</div>
      {{/each}}
    </script>
  </body>
</html>
like image 338
Thierry Lam Avatar asked Jun 02 '13 22:06

Thierry Lam


1 Answers

Try doing {{../site.name}} in your loop. The ../ Handlebars syntax allows access of the parent scope.

like image 86
Bojangles Avatar answered Jan 04 '23 09:01

Bojangles