简体   繁体   中英

Handlebars each helper arguments

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}}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM