You are here: Reference > Streams and JSP > JavaServer Pages tags > forEach

forEach JavaServer Page tag

Use the forEach tag to repeat an action for each property in a page, for each page in a Page List, or for each property in any list or group.

In runtime stream processing that includes a section or control that includes complex parameters on the Parameters tab, you can use the forEach tag to iterate over arrays or groups of parameter values.

Basics

Properties with a mode other than Single Value are known as aggregates. The forEach tag iterates through the parts of an aggregate property.

For example, assume a clipboard page named Operators contains embedded pxResults() pages. The pxResults() pages each hold a property pyUserIdentifier identifying one customer. Use the forEach tag to create an HTML table that contains a row for each customer:

<TABLE>
<pega:forEach name="Operators.pxResults">
  <TR><TD>
     <pega:reference name="$THIS.pyUserIdentifier" />
  </TD></TR>
</pega:forEach >
</TABLE>

Within the scope of the forEach tag, special keywords are available:

Complete syntax

In the syntax presentations below:

To do something for each property in a Value List, Value Group, Page, Page List, or Page Group, use this syntax. In place of the word aggregate-reference, enter the name of the aggregate property.

<pega:forEach name="aggregate-reference" >
      actionToRepeat
or
</pega:forEach >

Attribute

Description

name The name of the aggregate property, $this, or $this.propertyname. To refer to a complex parameter, use the keyword "param" followed by the complex parameter name.

To do some processing for each property in a top-level page, use this syntax:

<pega:forEach name="page-name" >
actionToRepeat

</pega:forEach >

If the array contains only Single Value properties, use the keyword $this:

<pega:forEach name="$this">
     actionToRepeat

</pega:forEach >

No conditional "break" or "leave" from the scope of a forEach tag is available: all iterations always occur. To perform only some, rather than all, iterations of a forEach tag, include a <pega:when > element:

<pega:forEach name="page-name" >
  <pega:when ...>
      actionToRepeat if true
  </pega:when>
</pega:forEach >

Nesting a forEach tag within the scope of another forEach tag is supported.

To iterate over complex parameters that are similar in structure to Value List, Value Group, Page List, and Page Group properties defined on the Parameters tab of a section or control rule:

<pega:forEach name="Param.MyPageList" >
     <pega:reference name="$this.Innervalue"/>
</pega:forEach>

Examples

These examples use the forEach tag to display values from clipboard pages.

Arrays within arrays

If the array contains lists of pages, then you can embed a forEach tag and iterate over that list as well:

<pega:forEach name=”.pxResults”>
    <pega:forEach name=”$this.addresses”>
           <pega:reference name=”$this.streetName”/>
     </pega:forEach>
</pega:forEach>

Obtain values for each assignment in a flow

To obtain values from an embedded group of pages:

<pega:forEach name=".pxFlow" >
     value="<pega:reference ref="$this.pxAssignmentKey" /> "
</pega:forEach >

This example uses the Work-.pxFlow property, a Page Group. It identifies, in sequence, the value of the pxAssignmentKey property for each page in the Page Group.

Using the forEach tag in control rules

To create a selection box, use syntax similar to the following:

<select name="<p:r n="$this-name" /> ">
<pega:forEach name=".pyKeyDefList" >
   <option value="<pega:reference ref="$this.pyKeyName" />">
<pega:reference ref="$this.pyKeyCaption" >
</pega:forEach >
</select>

To create option (radio) buttons, use syntax similar to the following:

<pega:withTarget name="$this" >
   <pega:forEach name=" .pyKeyDefList" >
        <input type="radio"  value="<pega:reference
             ref="$this.pyKeyName" / >">
        <pega:reference ref="$this.pyKeyCaption" / >
   </pega:forEach>
</pega:withTarget>

An example of iteration over complex parameter values rather than property values:

<pega:forEach name="Param.MyPageList">
   <pega:withEmbedded name="$this" >
     <pega:reference name=".MyInnerValue" />
   </pega:withEmbedded>
</pega:forEach>

JavaServer Page tags