0
votes

I am trying to include an object in an {{each}} iteration in handlebars which doesn't belong to the actual array object.

  • myArrayObj <-- array of json objects that include bunch of fields
  • someOtherObj <-- json object includes bunch of fields

First attempt:

{{#each myArrayObj}}

    {{#myHelper this.fieldInArrayObject someOtherObj.someField}} 
    {{/myHelper}}

{{/each}}

Helper:

Handlebars.registerHelper('myHelper', function (date, language) { console.log(JSON.stringify(language)); // <--- language is undefined });

someOtherObj.someField is coming out a undefined in the helper class? Everything else is working.

Another example:

{{something.field}} // <--- this works, it displays what I wish 

{{#each sessions}}
    {{something.field}} // <--- this does not work
{{/each}}
1

1 Answers

0
votes

when using object that do not belong the current scope, you'll need to use path

Try this

{{#each myArrayObj}}

    {{#myHelper this.fieldInArrayObject ../someOtherObj.someField}} 
    {{/myHelper}}

{{/each}}

For more on using paths - Look at Handlebars Paths on handlebarsjs.com