Foreach directive

 

Use the Foreach directive 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. Properties that have a mode other than Single Value are known as aggregates. The Foreach directive iterates through the parts of an aggregate property.

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

<TABLE>
  {foreach Operators.pxResults}
     <TR>
       <TD>{$THIS.pyUserIdentifier}</TD>
     </TR>
  {endforeach}
</TABLE>

JSP Equivalent

The pega:forEach JavaServer Page tag provides a functionally identical capability. See JSP Tags — forEach.

Complete syntax

In the syntax presentations below:

Use one of the three syntax variations.

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.

{foreach [IN[=]] aggregate-reference}
actionToRepeat

{end}

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

{foreach INPAGE [=] page-name}
actionToRepeat

{end}

Use a pipe character (|) to separate pages in the list-of-page-names. If no page name appears, the pipe identifies the current page. For example, A|B||C refers to four pages: A, B, the current page, and C. Similarly, X|Y| refers to three pages: X, Y, and the current page.

Examples

These examples use the Foreach directive to display values from clipboard pages.

Remember that $THIS refers to the current active property reference. You can use $THIS to display a read-only value or to display an input field. To use $THIS to display an input field, add the keyword input:

{$THIS input}.

Obtain values for each assignment in a flow

To use the Foreach directive to obtain values from an embedded group of pages into HTML, use this syntax:

{foreach in=.pxFlow}
value="{$this.pxAssignmentKey}"
{end}

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 directive in control rules

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

<select>
{foreach in .pyKeyDefList}
<option value="{$this.pyKeyName}">{$this.pyKeyCaption}
{end}
</select>

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

{with target=$this}
{foreach in .pyKeyDefList}
  <input type=radio
  value="{$this.pyKeyName}">{$this.pyKeyCaption}
{end}
{end}

Directives