each
You can iterate over a list using the each helper. Inside the block, you can use this to reference the element being iterated over.
Params
list{Array}- The list to iterate over
Usage
{{#each list}}
{{this}}
{{/each}}
Example
{{#each collection.products as |product|}}
{{ product.title }}
{{/each}}
else
You can optionally provide an else section which will display only when the list is empty.
{{#each collection.products as |product|}}
{{ product.title }}
{{else}}
No products
{{/each}}
@index
When looping through items in each, you can optionally reference the current loop index via {{@index}}.
{{#each collection.products as |product|}}
{{ @index }} - {{ product.title }}
{{/each}}
@first / @last
The first and last steps of iteration are noted via the @first and @last variables when iterating over an array.
{{#each collection.products as |product|}}
{{#if @first}}
{{ product.title }}
{{/if}}
{{#if @last}}
{{ product.title }}
{{/if}}
{{/each}}
Was this article helpful to you?