Today I learned that Handlebars.js has an {{else}}
instruction when you're iterating over a collection with {{each}}
. This allows you to make a contextualized check if the collection is empty.
This is great because it's one less block you'll have to deal with when validating state. So instead of the old ugly:
{{#if foos}}
There are no foos for you at this time.
{{else}}
{{#each foos as |foo|}}
LOOK AT THIS BEAUTIFUL FOO: {{foo.name}}
{{/each}}
{{/if}}
You can just:
{{#each foos as |foo|}}
LOOK AT THIS BEAUTIFUL FOO: {{foo.name}}
{{else}}
There are no foos for you at this time.
{{/each}}
Which is a lot more readable IMO👌 Read more about the built-in helpers here