Handlebars.js:从循环中索引对global.json的访问 [英] Handlebars.js: index access to global.json from a loop
问题描述
我有一个global.json文件:
I have a global.json file:
{
"title": "problem",
"dict": [
{
"name": "A",
"similar": [1, 2]
},
{
"name": "B",
"similar": [0]
},
{
"name": "C",
"similar": [1]
},
]
}
,我希望将A
的以下结果作为示例:
and I want the following result for A
as an example:
A
B C
我尝试使用以下车把脚本:
I tried to use the following handlebars script:
{{#with global.dict.[0]}}
{{name}}
{{#each similar}}
{{@root.global.dict.[this].name}}
{{/each}}
{{/with}}
输出仅为A
.但是,似乎this
在这里不能识别为整数用作索引
The output is is just A
. However, it seems this
is not recognized as integer here to used as the index
推荐答案
您将需要使用查找助手来查找 @root.global.dict
上的动态this
索引.然后,您将不得不进行第二次查找以获取结果的name
属性.另外,您可以使用 with 助手来为您提供范围为第一次查找的结果,如:
You will need to use the lookup helper to lookup the dynamic this
index on @root.global.dict
. You would then have to do a second lookup to get the name
property of the result. Alternatively, you could use the with helper to give you a block scoped to the result of the first lookup, as in:
{{#each similar}}
{{#with (lookup @root.global.dict this)}}
{{name}}
{{/with}}
{{/each}}
我创建了一个小提琴供您参考.
I have created a fiddle for your reference.
这篇关于Handlebars.js:从循环中索引对global.json的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!