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

Kev*_*ang 19 javascript handlebars.js

寻找一种方法来实现这一点:

{{#each someArray}}
  {{../otherObject.[this]}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

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

小智 17

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

{{#each someArray}}
  {{lookup ../otherObject this}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)


小智 5

一个可能的帮助解决方案:

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

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