如何根据集合的大小制作模板条件? [英] How do I make a template conditional based on a collection's size?

查看:84
本文介绍了如何根据集合的大小制作模板条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

 < template name =list> 
< ul>
{{#if items}}
{{#each items}}
< li> {{itemContents}}< / li>
{{/ each}}
{{else}}
< li class =placeholder>此列表中没有项目。< / li>
{{/ if}}
< ul>
< / template>

其中项目是一个Meteor.cursor:

  Template.list.items = function(){
return Items.find();
};

然而,上面的代码不起作用,因为即使没有项目(这是轻度惊讶,因为Handlebars评估 [] 为falsey)。我尝试将条件改为

  {{#if items.count}} 

但是我得到了一个神秘的错误

 未知帮助'物品'

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

解决方案

这是正确的方法:

 < template name =list> 
< ul>
{{#each items}}
< li> {{itemContents}}< / li>
{{else}}
< li class =placeholder>此列表中没有项目。< / li>
{{/ each}}
< ul>
< / template>

有关详细信息,请参阅 handlebarsjs.com

(流星使用 Spacebars ,它的灵感来自Handlebars,所以Syntax几乎相同。)


I want to do something like this:

<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 is a Meteor.cursor:

Template.list.items = function() {
  return Items.find();
};

However, the code above doesn't work, as the conditional will evaluate positively even if there are no items (which is mildly surprising because Handlebars evaluates [] as falsey). I tried changing the condition to

{{#if items.count}}

but then I get the cryptic error

Unknown helper 'items'

So, is there a way to write such a condition within a meteor Handlebars template?

解决方案

This would be the correct way to go:

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

For further information take a look at handlebarsjs.com.

(Meteor uses Spacebars which is inspired by Handlebars. So the Syntax is almost the same.)

这篇关于如何根据集合的大小制作模板条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆