kalabo
kalabo

Reputation: 564

Handlebars js - cannot access parent object properties from loop / partial

I have the following data object:

enter image description here

And with this i am using the following Loop with no problem

Templates.Links = [
"<ul>",
"{{#each data}}",
    "<li>",
    "<div class='flex-item-button'>",
        "<div class='info noicon'>",
            "<span class='name'>{{{URL}}}</span>",
            "<span class='meta'>{{URL}}</span>",                                
        "</div>",
        "<div>",                
        "<span class='expand expand-item-details'><i class='fa fa-sort-down'></i></span>",
        "</div>",
        "<div class='additional-info'>",

        "</div>",
    "</div>",       
    "</li>",
"{{/each}}",
"</ul>",    
].join("\n");

This is working with no problems and withing the Each data loop i can access my data properties. What i want to be able to do however is access the title / caml fields from within my loop. I have tried the following with no luck.

{{./title}}
{{../title}}

I ideally wanted to use a partial within my each loop and then from within that partial access the parent node but that didn't appear to work either.

Upvotes: 0

Views: 1006

Answers (1)

intale
intale

Reputation: 831

Here is the solution:

{{#each data}}
  ....
  {{#with ../this}}
    {{> yourPartial}}
  {{/with}}
  ....
{{/each}}

And in your partial, simply:

{{title}}

Edit. You should implement your own helper, like:

Handlebars.registerHelper("withCurrentItem", function(context, options){
    var contextWithCurrentItem = context;
    contextWithCurrentItem.currentItem = options.hash.currentItem;
    return options.fn(contextWithCurrentItem);
});

And use it like:

{{#withCurrentItem ../this currentItem=this}}
  {{> yourPartial}}
{{/withCurrentItem}}

Upvotes: 3

Related Questions