Handlebars.js:从循环中索引访问 global.json
Handlebars.js: index access to global.json from a loop
我有一个 global.json 文件:
{
"title": "problem",
"dict": [
{
"name": "A",
"similar": [1, 2]
},
{
"name": "B",
"similar": [0]
},
{
"name": "C",
"similar": [1]
},
]
}
我想要 A
的以下结果作为示例:
A
B C
我尝试使用以下车把脚本:
{{#with global.dict.[0]}}
{{name}}
{{#each similar}}
{{@root.global.dict.[this].name}}
{{/each}}
{{/with}}
输出只是 A
。但是,这里似乎 this
不被识别为整数用作索引
您将需要使用查找帮助器到 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 帮助器来为您提供一个范围为第一次查找结果的块,如:
{{#each similar}}
{{#with (lookup @root.global.dict this)}}
{{name}}
{{/with}}
{{/each}}
我创建了一个fiddle供您参考。
我有一个 global.json 文件:
{
"title": "problem",
"dict": [
{
"name": "A",
"similar": [1, 2]
},
{
"name": "B",
"similar": [0]
},
{
"name": "C",
"similar": [1]
},
]
}
我想要 A
的以下结果作为示例:
A
B C
我尝试使用以下车把脚本:
{{#with global.dict.[0]}}
{{name}}
{{#each similar}}
{{@root.global.dict.[this].name}}
{{/each}}
{{/with}}
输出只是 A
。但是,这里似乎 this
不被识别为整数用作索引
您将需要使用查找帮助器到 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 帮助器来为您提供一个范围为第一次查找结果的块,如:
{{#each similar}}
{{#with (lookup @root.global.dict this)}}
{{name}}
{{/with}}
{{/each}}
我创建了一个fiddle供您参考。