Handlebars.js: How to set {{#each }} iterate value from helper

Created on 11 Apr 2019  路  2Comments  路  Source: handlebars-lang/handlebars.js

Usually for a {{#each}} block, we can set the value directly in it like: {{#each users}}:

users: [
    {name: 'foo'},
    {name: 'bar'}
]

What I need to do is do be able to set the {{#each}} iteration value from a helper.

I have tried with this helper:

Handlebars.registerHelper('myHelper', function () {
  return new Handlebars.SafeString('users');
});

## also tested with:
Handlebars.registerHelper('myHelper', function () {
  return 'users';
});

and in my view:

{{#each (myHelper)}} # and {{#each myHelper}}
  <p>{{name}}</p>
{{/each}}

But nothing get shown. Any help is much appreciated!

Most helpful comment

This might work:

{{#each (lookup . (myHelper))}}

But you also have to make sure that you are returning the name of your property from the helper. Currently, it's users in the helper and user in the object.

All 2 comments

This might work:

{{#each (lookup . (myHelper))}}

But you also have to make sure that you are returning the name of your property from the helper. Currently, it's users in the helper and user in the object.

Thanks a lot @nknapp 馃 you're the man

Was this page helpful?
0 / 5 - 0 ratings