问题描述
我有以下数据结构:
{
things:[
desk,
椅子,
笔,
书,
灯
],
所有者:[
Julia ,
Sandra,
John,
Paul
]
}
$ b $ p 什么工作: handleblars
b
$ b pre $ {code} {{#each things}}
< p>这{{this} }属于{{lookup ../owners @index}}< / p>
{{/ each}}
正确输出:
这张桌子属于Julia
这张椅子属于Sandra
这支笔属于John
这本书属于Paul
此灯属于
什么不起作用:
现在,我想添加一个条件,因为最后一个事物
可能没有 owner
。然后,模板将如下所示:
{{#each things}}
{{#if lookup .. / owners @index}}
< p>此{{this}}属于{{lookup ../owners @index}}< / p>
{{else}}
< p> ...但这{{this}}属于任何人< / p>
{{/ if}}
{{/ each}}
输入:
这张桌子属于Julia
这把椅子属于Sandra
这支笔属于John
这本书属于保罗
......但是这个灯属于无人
不幸的是,这个 {{#if lookup ../owners @index}}
这个东西不能用。
我的问题:是否有可能通过内置的Handlebars助手实现这一点,还是我必须编写一个自定义助手?
我认为如果你想改变你的数据结构会更好,比如像这样:
<$ c
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
所有者:Sandra
},
{
东西:笔,
所有者:John},
{
thing:book,
owner:Paul},
{
thing:lamp
}
]
那么你的车把模板看起来就像
{{#this this}}
{{#if this.owner}}
< p>这{{this.thing}}属于{ this.owner}}< / p为H.
{{else}}
< p> ...但这{{this.thing}}属于任何人< / p>
{{/ if}}
{{/ each}}
this会输出(我在上运行它)
< p>这张桌子属于Julia< / p>
< p>这张椅子属于Sandra< / p>
< p>这支笔属于John< / p>
< p>本书属于Paul< / p>
< p> ...但此灯属于任何人< / p>
使用手柄助手可能看起来不错,但是将逻辑从手柄移动到javascript / strong>长远来说会更好。
I have the following data structure:
{
things: [
"desk",
"chair",
"pen",
"book",
"lamp"
],
owners: [
"Julia",
"Sandra",
"John",
"Paul"
]
}
What's working:
This handleblars
template:
{{#each things}}
<p>This {{this}} belongs to {{lookup ../owners @index}}</p>
{{/each}}
Correctly outputs:
This desk belongs to Julia
This chair belongs to Sandra
This pen belongs to John
This book belongs to Paul
This lamp belongs to
What's not working:
Now, I'd like to add a condition because the last thing
might not have an owner
. The template would then look something like:
{{#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}}
And the ouput:
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
Unfortunately, this {{#if lookup ../owners @index}}
thing doesn't work.
My question: is it possible to achieve this with built-in Handlebars helpers, or do I have to write a custom helper?
I think it will be better if you were to change your data structure, for instance like this:
[
{
thing: "desk",
owner: "Julia"
},
{
thing: "chair",
owner:"Sandra"
},
{
thing: "pen",
owner: "John"},
{
thing: "book",
owner: "Paul"},
{
thing: "lamp"
}
]
then your handlebar template will look like
{{#each this}}
{{#if this.owner}}
<p>This {{this.thing}} belongs to {{ this.owner}}</p>
{{else}}
<p>...But this {{this.thing}} belongs to nobody</p>
{{/if}}
{{/each}}
this will output (I ran it on http://tryhandlebarsjs.com/)
<p>This desk belongs to Julia</p>
<p>This chair belongs to Sandra</p>
<p>This pen belongs to John</p>
<p>This book belongs to Paul</p>
<p>...But this lamp belongs to nobody</p>
Using handlebars helpers may look good to you, but moving logic from handlebars to javascript will be better in long run.
这篇关于基于查找的把手条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!