Skip to content

<forEach>

A tag that iterates over a collection and inflates its children for each item. The current item is stored as a dependency using the name specified by the var attribute.

The <forEach> tag supports iterating over List, Set, and Map collections. When iterating over a Map, each item is converted to a map with key and value entries.

Attributes

Name Type Description Required Default
var String The dependency key to store the current item. Yes -
items Iterable The collection to iterate over. Supports EL expressions. When null, the tag renders nothing. No null
indexVar String The dependency key to store the current index. No null
groupSize int Number of items per group. When greater than 1, var receives a list of items instead of a single item. No 1
dependenciesScope String How to scope dependencies: new, copy, or inherit. No copy

Examples

Basic Iteration

<Column>
    <forEach var="user" items="${users}">
        <Text data="${user.name}"/>
    </forEach>
</Column>

With Index

<Column>
    <forEach var="item" items="${items}" indexVar="index">
        <Text data="${toString(index)}: ${item.name}"/>
    </forEach>
</Column>

Iterating Over a Map

When iterating over a Map, each item is a map with key and value entries:

<Column>
    <forEach var="entry" items="${settings}">
        <Row>
            <Text data="${entry.key}"/>
            <Text data="${entry.value}"/>
        </Row>
    </forEach>
</Column>

Grouped Items

Use groupSize to create groups of items — useful for multi-column layouts:

<Column>
    <forEach var="group" items="${items}" groupSize="2">
        <Row>
            <forEach var="item" items="${group}">
                <Expanded>
                    <Text data="${item.title}"/>
                </Expanded>
            </forEach>
        </Row>
    </forEach>
</Column>

Conditional Items

<Column>
    <forEach var="product" items="${products}">
        <if test="${product.isAvailable}">
            <Card>
                <Text data="${product.name}"/>
            </Card>
        </if>
    </forEach>
</Column>

See Also

  • <forLoop> - For numeric iteration
  • <if>/<else> - For conditional rendering
  • <List> - For creating collections