"guests": [
{
"guestname": "john smith",
"is_active": false,
"guestheight": "175cm",
"guestprofile": "https://www.example.com"
},
{
"guestname": "david smart",
"is_active": false,
"guestheight": "175cm"
}
]
我想检查一下是否存在访客配置文件。考虑到我目前有一个变量,其中包含我们试图访问的列表的索引,即itemIndex
。因此,基本上我尝试在handlers.js上下文中查询if guests[itemIndex]["guestprofile"]
。
如果我直接参考
{{#if guests.0.guestprofile}}
//do something
{{/if}}
它运行良好。然而,如果我用下面这样的itemIndex替换0,一切都会崩溃。。。
{{#if guests.itemIndex.guestprofile}}
//do something
{{/if}}
目前我也尝试过
{{#if (lookup guests itemIndex).guestprofile}}
//do something
{{/if}}
{{#if guests[itemIndex].guestprofile }}
//do something
{{/if}}
它们都没有真正起作用。请帮忙,提前谢谢!
您因此通过lookup
尝试关闭。
问题是Handlebars不允许像使用(lookup guests itemIndex).guestprofile
那样将键链接到查找子表达式上。它只是没有以这种方式解析动态变量,这与必须使用查找而不是guests.itemIndex.guestprofile
的原因相同。
您遗漏的部分是,您实际上需要两个查找——一个是在itemIndex
处获取guests
的元素,另一个是获取该元素的guestprofile
。
您的模板需要成为:
{{#if (lookup (lookup guests itemIndex) 'guestprofile')}}
do something
{{/if}}
我制作了一把小提琴供你参考。