each
Like a loop, you can iterate over a list using the built-in #each
helper. Inside the block, you can use this to reference the element being iterated over.
{{#each people}} {{this}} {{/each}}
when used with this context:
{ people: [ "Yehuda Katz", "Alan Johnson", "Charles Jolley", ], }
will result in:
Yehuda Katz Alan Johnson Charles Jolley
this
You can use the this
expression in any context to reference the current context.
You can optionally provide an else
section which will display only when the list is empty.
{{#each paragraphs}} {{this}} {{else}} No content {{/each}}
@index
When looping through items in each, you can optionally reference the current loop index via {{@index}}
.
{{#each array}} {{@index}}: {{this}} {{/each}}
@key
Additionally for object iteration, {{@key}}
references the current key name:
{{#each object}} {{@key}}: {{this}} {{/each}}
First and last
The first and last steps of iteration are noted via the @first
and @last
variables when iterating over an array.
Nested Blocks
Nested each blocks may access the iteration variables via depth based paths. To access the parent index, for example, {{@../index}}
can be used.