Skip to content

Template Iteration

tporcham edited this page Jan 30, 2019 · 2 revisions

Templates allow you to create in-template iterations which allow you to enumerate collections, arrays, and lists.

The Foreach Orb-tag

The foreach tag is a regular orb-tag with a keyword preceding the @. But it has some special semantics.

Consider the following example:

@foreach{users}
- @{item.name} (Wage: @{item.salary})
@end{}

As a block token in MVEL templates, the block must be terminated with @end{}.

In the above example we iterate over a list of objects named users. By default, the element from the collection on each iteration is named item. However, you may use the as keyword to re-assign it to something else to deal with nesting:

@forach{users as user}
Name: @{user.name}
Roles:
  @foreach{user.roles as role}
  - @{role.name}
  @end{}
@end{}

MVEL also allows you to iterate over multiple collections, as long as they are each the same size. This can be particularly useful when generating method signatures.

sometMethod(@foreach{types, identifiers as type, identifier}type identifier@end{","})

The string supplied, @end{","}, at the end is optional and is the delimiter used between each iteration.

See Also