Looking for a way to access to achieve this:
{{#each someArray}} {{../otherObject.[this]}} {{/each}}
How do I evaluate the value of this
and then reference it as a key to my object otherObject
?
Answer: Use the Square Bracket ( [] ) Notation There are two ways to access or get the value of a property from an object — the dot ( . ) notation, like obj. foo , and the square bracket ( [] ) notation, like obj[foo] .
Handlebars compiles templates into JavaScript functions. This makes the template execution faster than most other template engines.
A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}
With lookup: https://handlebarsjs.com/guide/builtin-helpers.html#lookup
{{#each someArray}} {{lookup ../otherObject this}} {{/each}}
One possible solution with a helper:
/* {{#each someArrayOfKeys}} {{#withItem ../otherObject key=this}} {{this}} {{/withItem}} {{/each}} */ Handlebars.registerHelper('withItem', function(object, options) { return options.fn(object[options.hash.key]); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With