Skip to content

Loops

Use for loops inside .mgx to generate repeated sections.

@state items = ["apple", "banana", "cherry"]

for i in items:
    <<Iteration ${i}>>

The prompt would be loaded with

Iteration apple
Iteration banana
Iteration cherry

Dictionary Iteration

Use for key, value in dict_var: to iterate over both keys and values of a dictionary.

@state person = {"name": "Alice", "role": "admin"}

for key, value in person:
    <<${key}: ${value}>>

The prompt would be loaded with

name: Alice
role: admin