Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handlerbars.js check if list is empty

If you have something that you want to display once and only if the array has data, use

{{#if items.length}}
    //Render
{{/if}}

.length will return 0 for empty arrays so we have achieved a real falsey value.


The "each" tag can take an "else" section too. So the simplest form is:

{{#each items}}
// render item
{{else}}
// render empty
{{/each}}

Ok it's simpler than I thought:

{{#if items}}
// render items

{{#each items}}
// render item
{{/each}}

{{else}}
// render empty
{{/if}}

If you want to check if a collection (cursor) is empty or not, the previous answers won't be useful, instead you must use the count() method :

{{#if items.count}}
    <p>There is {{items.count}} item(s).</p>
{{else}}
    <p>There is nothing</p>
{{/if}}