Skip to content

Loops and lists

Use $each to repeat text for every item in a list. The name after as receives the current item; an optional second name receives its index.

$each(["News", "Chat"] as @item, @index)
$(@index + 1). @item
$end

Result:

1. News
2. Chat

Items follow list order. Adding one to the zero-based index produces the numbering shown to readers.

Item and index names must differ. They shadow matching outer names without replacing the outer values.

A local first declared in the body is initialized again each iteration. To accumulate a result, declare it before the loop, as shown in local variables.

The first binding receives the value and the second receives the key. The loop visits keys in ordinal text order, regardless of the order in which you wrote them.

$set(@rooms, { music: "Room 2", drawing: "Room 1", art: nil })
$each(@rooms as @room, @workshop)
$if(!@room)
$continue
$end
$upper(@workshop): @room
$end

Result:

DRAWING: Room 1
MUSIC: Room 2

$continue skips the workshop with no room. Use $break to leave the nearest loop, or $stop to finish the current field and keep its output.

Nil runs zero iterations, so an optional list does not need a fabricated replacement. Other scalars are not iterable, and collections cannot be mutated during iteration.

Nested loops share the field’s execution allowance. If you only need comma-separated text, $join is often simpler; see text and number functions.