Skip to content

for · Step looping

Execute a step multiple times by looping over one or more 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

Instead of defining a dictionary with a single key, you may use a more concise syntax when looping over a single list and access the current value using the __ variable.

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 used. As execution is performed from outer (first property) to inner (last property), the order does matter.

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), you may use variables defined earlier 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 MIT License.