我有以下数据结构:
{
things: [
"desk",
"chair",
"pen",
"book",
"lamp"
],
owners: [
"Julia",
"Sandra",
"John",
"Paul"
]
}
什么在起作用:
这个handleblars
模板:
{{#each things}}
<p>This {{this}} belongs to {{lookup ../owners @index}}</p>
{{/each}}
正确输出:
This desk belongs to Julia
This chair belongs to Sandra
This pen belongs to John
This book belongs to Paul
This lamp belongs to
什么不起作用:
现在,我想添加一个条件,因为最后一个 thing
可能没有 owner
。模板将类似于:
{{#each things}}
{{#if lookup ../owners @index}}
<p>This {{this}} belongs to {{lookup ../owners @index}}</p>
{{else}}
<p>...But this {{this}} belongs to nobody</p>
{{/if}}
{{/each}}
输出:
This desk belongs to Julia
This chair belongs to Sandra
This pen belongs to John
This book belongs to Paul
...But this lamp belongs to nobody
不幸的是,这个{{#if lookup ../owners @index}}
不起作用。
我的问题:是否可以使用内置的 Handlebars 助手来实现这一点,还是我必须编写自定义助手?
最佳答案
关于javascript - 基于查找的 Handlebars 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483744/