Handlebars.js - 使用变量键访问对象值

时间:2013-10-28 22:27:58

标签: javascript handlebars.js

寻找实现此目的的方法:

{{#each someArray}}
  {{../otherObject.[this]}}
{{/each}}

如何评估this的值,然后将其作为对象otherObject的键引用?

2 个答案:

答案 0 :(得分:16)

使用查询:http://handlebarsjs.com/builtin_helpers.html#lookup

{{#each someArray}}
  {{lookup ../otherObject this}}
{{/each}}

答案 1 :(得分:5)

帮助者的一种可能解决方案:

/*
{{#each someArrayOfKeys}}
  {{#withItem ../otherObject key=this}}
    {{this}}
  {{/withItem}}
{{/each}}
*/

Handlebars.registerHelper('withItem', function(object, options) {
    return options.fn(object[options.hash.key]);
});