如何根据集合的大小创建有条件的模板?

2024-02-05

我想做这样的事情:

<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>

where 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>

欲了解更多信息,请查看车把js.com http://handlebarsjs.com/.

(流星使用空格键 https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md其灵感来自车把。所以语法几乎是一样的。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何根据集合的大小创建有条件的模板? 的相关文章