This helper allows you to iterate over a list. Inside the #each block, you can reference the element to be iterated over.
{{#each field}}{{this}}{{/each}}
This example shows a simple record with names. The {{each}} expression will reference the context while the {{this}} expression will iterate over the array of items in the [people] array.
For the example below, in integrator.io, the [people] array must be referenced as an absolute path in the Resource path field. If the context was an object (name:value pair), then the path would not need to be set.
Also in this example, the semicolon and space between the expressions will allow spacing and punctuation between the variables on output.
|
Template |
{{#each people}}{{this}}; {{/each}} |
|---|---|
|
Context |
{
"people": [
"Bertram Gilfoyle",
"Erlich Bachman",
"Jin Yang"
]
} |
|
Output |
Bertram Gilfoyle; Erlich Bachman; Jin Yang; |
This example shows the system iterating over the array in the "field.names" variables.
|
Template |
{{#each fields.names}}{{#each this}} Employee {{@key}}: {{this}}
{{/each}}{{/each}} |
|---|---|
|
Context |
{
"fields": {
"field": "sample",
"field2": "row2",
"rights": [
"system",
"admin",
"editor",
"contributor",
"viewer",
"vendor"
],
"names": [
{"name1":"Stacy"},
{"name2":"Lance"},
{"name3": "Bernice"}
]
}
} |
|
Output |
Employee name1: Stacy Employee name2: Lance Employee name3: Bernice |
When looping through items in #each, you can also reference the current loop index.
|
Template |
System privilege levels: {{@index}}
{{#each fields.rights}}
{{@index}}: {{this}}
{{/each}} |
|---|---|
|
Context |
{
"fields": {
"field": "sample",
"field2": "row2",
"rights": [
"system",
"admin",
"editor",
"contributor",
"viewer",
"vendor"
],
"names": [
{"name1":"Stacy"},
{"name2":"Lance"},
{"name3": "Bernice"}
]
}
} |
|
Output |
System privilege levels: 0: system 1: admin 2: editor 3: contributor 4: viewer 5: vendor |
Additionally for iterating over objects, {{@key}} will reference the current key name. @key will
provide the current index location in an array or the key names in the Context.
|
Template |
{{#each fields.rights}}
{{@key}}:{{this}};
{{/each}} |
|---|---|
|
Context |
{
"fields": {
"field": "sample",
"field2": "row2",
"rights": [
"system",
"admin",
"editor",
"contributor",
"viewer",
"vendor"
],
"names": [
{"name1":"Stacy"},
{"name2":"Lance"},
{"name3": "Bernice"}
]
}
} |
|
Output |
0:system; 1:admin; 2:editor; 3:contributor; 4:viewer; 5:vendor; |
The first and last steps of iteration are noted via the @first and @last variables when iterating over an array. When iterating over an object, only @first is available. Nested #each blocks may access the iteration variables via depth based paths. To access a parent index in a nested #each loop, specify @../index.
The each helper also supports block parameters, allowing for named references anywhere in the block.
|
Template |
{{#each fields.rights}}
{{@key}}:{{this}};
{{/each}} |
|---|---|
|
Context |
{
"fields": {
"field": "sample",
"field2": "row2",
"rights": [
"system",
"admin",
"editor",
"contributor",
"viewer",
"vendor"
],
"names": [
{"name1":"Stacy"},
{"name2":"Lance"},
{"name3": "Bernice"}
]
}
} |
|
Output |
0:system; 1:admin; 2:editor; 3:contributor; 4:viewer; 5:vendor; |
To create a list or convert data based on index location in a record, you can declare an |item| using vertical bars (pipe character), then entering the characters by their respective index locations (where 0=first character).
|
Template |
{{#each inventoryItems as |item|}}
{{item.[0]}}: {{item.[10]}}
{{/each}} |
|---|---|
|
Context |
{
"inventoryItems": {
"X": "1001 text a",
"Y": "2002 text b",
"Z": "3003 text c"
}
} |
|
Output |
1: a 2: b 3: c |