handlebars.js - Dynamic paths in handlebars -
i searched through lot of questions hasn't found answer. use handlebars templates , have data structure:
{ privileged_users: [ "user1", "user2" ], users: { user1: { name: "n1" }, user2: { name: "n2" }, user3: { name: "n3" } } }
i wan't output privileged users template. this:
<table> {{#each privileged_users}} <tr><td>{{../users.[this].name}}</td></tr> {{/each}} </table>
is possible without additional helpers?
if isn't how can write block helper changing context ../users.[this] ?
register following helper:
handlebars.registerhelper('lookupprop', function (obj, key, prop) { return obj[key] && obj[key][prop]; });
then modify template like:
<table> {{#each privileged_users}} <tr><td>{{lookupprop ../users 'name'}}</td></tr> {{/each}} </table>
here working fiddle. previous 1 simple expression helper.
now here working jsfiddle according question.
handlebars has built-in lookup helper since version 3.0.3. alternate block helper handlebars partial context approach i.e. define/register partial , use different context in main template.
Comments
Post a Comment