I often do something like this, using the items
helper twice:
{{#if items}}
<h1>Items</h1>
{{#each items}}
{{> item}}
{{/each}}
{{/if}}
Template.foo.helpers
items: ->
Items.find
bar: true
,
sort: created: -1
transform: (item) ->
i.good = true
i
Is Meteor doing extra work in this scenario? Would it be more efficient to switch the if
to use something like areItems
?
areItems: ->
Items.find
bar: true
.count() > 0
You can do what you want using spacebars'
#with
block tag.Like this:
The block tag is documented here. The relevant quote is:
update: fixed code for desired behaviour. Also while my example demonstrates making one helper call it is better practice to make an 'itemList' template and include it by using {{> itemList items}}.
You can use
{{else}}
In the template, you can use {{#with items}} and then either 'this.count' or 'this.length' to check whether your helper returned any items.
Use this.count if 'items' is a cursor, e.g. the result of a find() operation:
Use this.length if 'items' is an array:
Use
#with
,#if this.length
, and.fetch
:I found that simply
{{#if items.count}}
was sufficient.