admin管理员组文章数量:1289528
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 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?
Share Improve this question asked Dec 27, 2015 at 18:58 JivanJivan 23.1k16 gold badges92 silver badges142 bronze badges 1- Seems like the answer to your question is still a 'no'. Please let me know if you managed to solve this or whether you have written a custom Handlebars helper? – Gibin Ealias Commented Feb 18, 2018 at 21:29
4 Answers
Reset to default 9You can indeed do just what you're trying to do, using subexpressions:
{{#if (lookup ../owners @index)}}
works like a charm. (source: Handlebars website)
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./)
<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.
I believe the answer is a 'no' if you want to nest Handlebars lookup
with an if
.
But here, if you want to omit the last thing
(or n things) which don't have an owner
, you may reverse check the #each
like below,
{{#each owners}}
<p>This {{lookup ../things @index}} belongs to {{this}}</p>
{{/each}}
Which outputs,
<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>
Hope this helps.
I have found an alternate solution by writing a custom helper, isIndexExist
.
Handlebars.registerHelper("isIndexExist", function(array, value, options) {
return value < array.length ? options.fn(this) : options.inverse(this);
});
And in the template, you can write,
{{#each things}}
{{#isIndexExist ../owners @index}}
<p>This {{this}} belongs to {{lookup ../owners @index}}</p>
{{else}}
<p>...But this {{this}} belongs to nobody</p>
{{/isIndexExist}}
{{/each}}
Hope this helps.
本文标签: javascriptHandlebars condition based on lookupStack Overflow
版权声明:本文标题:javascript - Handlebars condition based on lookup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741435492a2378611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论