在把手中寻找助手

chr*_*vdb 2 handlebars.js

我有一个'countries'对象,我作为Handlebars上下文的一部分传递:

{
 'hk': {'name': 'Hong Kong', 'someotherprop': 'someothervalue'},
 'us': {'name': 'United States',  'someotherprop': 'yetanothervalue'},
 ...
}
Run Code Online (Sandbox Code Playgroud)

我想使用查找Handlebar助手从国家代码'hk'中找到'香港'这个名字.

我可以得到以下对象

{'name': 'Hong Kong', 'someotherprop': 'someothervalue'}
Run Code Online (Sandbox Code Playgroud)

使用以下Handlebars指令

{{lookup ../countries countrycode}}
Run Code Online (Sandbox Code Playgroud)

但是我现在如何从这个对象中提取name属性呢?

chr*_*vdb 9

显然,可以使用子表达式语法链接查找调用

{{lookup (lookup ../countries countrycode) 'name'}}
Run Code Online (Sandbox Code Playgroud)

  • 而且,如果你需要访问多个属性,例如`name`和`someotherprop`,你可以使用`{{#with(lookup ../countries countrycode)}} {{name}} {{someotherprop}} { {/ with}}`http://stackoverflow.com/a/42520682/1487413 (2认同)