Skip to content

for · Step iterations

Execute a step multiple times by looping over lists of values.

yaml
- _: 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.

yaml
- _: 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.

yaml
- _: 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 iterations
  • i: current iteration index (0-based)
  • I: current iteration index (1-based, same as i + 1)
  • first: whether this is the first iteration (same as i === 0)
  • last: whether this is the last iteration (same as i === 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.

yaml
- _: 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).

yaml
- _: 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.

yaml
- log: ${greeting}
  # "Bonjour, Gustave !"
  # "Bonjour, Maëlle !"
  let:
    greeting: Bonjour, ${member} !
  for:
    member: [ Gustave, Maëlle ]

Released under the AGPL-3.0-or-later License.