gpt4 book ai didi

meteor - 如何根据集合的大小使模板有条件?

转载 作者:行者123 更新时间:2023-12-03 21:02:59 25 4
gpt4 key购买 nike

我想做这样的事情:

<template name="list">
<ul>
{{#if items}}
{{#each items}}
<li>{{itemContents}}</li>
{{/each}}
{{else}}
<li class="placeholder">There are no items in this list.</li>
{{/if}}
<ul>
</template>

哪里 items是 Meteor.cursor:
Template.list.items = function() {
return Items.find();
};

但是,上面的代码不起作用,因为即使没有项目,条件也会评估为正(这有点令人惊讶,因为 Handlebars 评估 [] 为假)。我尝试将条件更改为
{{#if items.count}}

但后来我得到了一个神秘的错误
Unknown helper 'items'

那么,有没有办法在 meteor Handlebars 模板中编写这样的条件?

最佳答案

这将是正确的方法:

<template name="list">
<ul>
{{#each items}}
<li>{{itemContents}}</li>
{{else}}
<li class="placeholder">There are no items in this list.</li>
{{/each}}
<ul>
</template>

如需更多信息,请查看 handlebarsjs.com .

(Meteor 使用 Spacebars,其灵感来自 Handlebars。所以语法几乎相同。)

关于meteor - 如何根据集合的大小使模板有条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com