for · Step iterations
Execute a step multiple times by looping over lists of values.
- _: Say hi to each member
log: Bonjour, ${member} !
for:
member: [ Gustave, Maëlle, Lune, Sciel ]Each key in the for block is exposed as a variable of the same name in the current scope, successively taking each value from the corresponding list.
TIP
Iterating over a single list of value can be achieved with a more concise syntax, using the special __ variable to represent the current value.
- _: Say hi to each member
log: Bonjour, ${__} !
for: [ Gustave, Maëlle, Lune, Sciel ]Nested loops
Multiple properties in a for effectively creates nested loops, where each combination of values will be processed. As executions are performed from outer (first property) to inner (last property), the order of properties does matter to some extent.
- _: Say hi to each member of each expedition
log: Bonjour, ${member} from expedition ${expedition} !
for:
expedition: [ 33 ]
member: [ Gustave, Maëlle, Lune, Sciel ]Accessing loop metadata
When inside a looped step, esquie.for provides additional information about the current iteration:
n: total number of iterationsi: current iteration index (0-based)I: current iteration index (1-based, same asi + 1)first: whether this is the first iteration (same asi === 0)last: whether this is the last iteration (same asi === n - 1)
Looped identified steps
When using the for in conjunction with the id property, all the generated sub-steps are grouped into an array.
- _: Explaining looped identified steps
id: expeditions
for:
expedition: [ 37, 36, 35, 34, 33]
- asserts: Array.isArray(esquie.steps.expeditions)About templating order
Since records are templatized depth-first and in declaration order (as explained in the templating rules), variables defined earlier may be reused in later variables (even if they are defined in the same for block).
- _: Showcasing templating order
for:
a: [ foo, bar ]
b: ${a.split("")}
# 1 | a: "foo" | b: "f"
# 2 | a: "foo" | b: "o"
# 3 | a: "foo" | b: "o"
# 4 | a: "bar" | b: "b"
# 5 | a: "bar" | b: "a"
# 6 | a: "bar" | b: "r"CAUTION
for and let properties (in this order) are exceptions to the general templating order rule, as they are templatized before any other property in the same step.
- log: ${greeting}
# "Bonjour, Gustave !"
# "Bonjour, Maëlle !"
let:
greeting: Bonjour, ${member} !
for:
member: [ Gustave, Maëlle ]