Software AG Products 10.7 | Integrating On-Premises and Cloud Applications | DSPs and building output templates | Using Output Templates to Format Service Output | %loop%
 
%loop%
You use the %loop% tag to repeat a block of code once for each element in a specified array (String list or document list) or for each key in a document.
Syntax
%loop [Variable] [option option
option...]
%
Block of Code
%end%
Arguments
Variable specifies the name of the array variable over which you want the enclosed section of code to iterate.
*You may optionally omit Variable and specify the –struct option to loop over each element in the current scope.
*When looping against a set of complex elements (e.g., documents, document lists, String lists) in a document, you can optionally use the #$key keyword to specify Variable instead of explicitly specifying a key name. When you use #$key in place of an explicit key name, it indicates that you want to apply the body of the loop to (i.e., switch the scope to) the current key. This allows you to process the contents of a document whose key names are not known. It is most often used with the –struct option to process a set of documents contained within another document. For an example of how this option is used, see the examples, below.
If Variable is a...
The loop is applied to...
String list
Each String in the list.
Document list
Each document in the document list.
Document
Each key in the document. When you use %loop% to process the elements of a document, you must also use the –struct option in the %loop% tag.
Options
You can use any of the following options with this tag. To specify multiple options, separate the options with spaces.
Options
Description
-struct
Specifies that Variable is a document and instructs the server to apply the loop once to each key in that document.
When you use the –struct option, you can use the $key variable to retrieve the name of each element in the document. See examples, below.
-eol
Ends the body of the loop at the next end-of-line (EOL) character in the code. When you use –eol, you can omit the %end% tag.
-$index
Returns the current index number in an array. You can use it within a loop to obtain the index number upon which the loop is acting during each iteration.
Effect on Scope
If Variable is a document list, scope switches to the document on which the loop is executing.
Notes
Omit the variable name from the %value% tag if it is used in the body of a loop for a String table or a document. When variable name is omitted, the server inserts the value of the current element at run time.
For readability, you can optionally use %endloop% instead of %end% to denote the end of a %loop% block.
Examples
The following example generates a paragraph for each document in the items document list.
.
.
.
<p>This shipment contains the following items:</p>
%loop items%
<p>%value qty% %value stockNum% %value description% %value status%</p>
%end%
.
.
.
The following example generates a line for each element in the backItems String list.
.
.
.
<p>The following items are backordered</p>
<p>
%loop backItems%
%value%<BR>
%end%
</p>
.
.
.
The following example shows how you can nest %loop% tags to process the individual document elements in a document list.
.
.
.
<This shipment contains the following items:</p>
<table>
%loop items%
<tr>
%loop -struct%
<td>%value%</td>
%end%
</tr>
%end%
.
.
.
The following example shows how you can use the %loop% tag to dump the contents (key names and values) of the current scope.
.
.
.
%loop -struct%
%value $key% %value%<BR>
%end%
.
.
.
The following example shows how you use the #$key option to loop over the individual elements in a collection of documents contained within another document.
This example assumes that the service returns a document named MatchingAddress
that holds a set of documents (of unknown name and quantity) containing
address information.
.
.
.
<p>The following addresses were returned:</p>
%loop -struct MatchingAddresses%
%loop -struct #$key%
<p>
%value streetAddr1%
%value streetAddr2%
%value city%, %value state% %value postalCode%
</p>
%end%
%end%
.
.
.
The following example shows how to obtain the iteration number of the loop execution and insert it into the output:
indices = {%loop arrayA%%value $index%%loopsep ’,’%%endloop%}
When ArrayA has three elements, the output will be:
indices = {0,1,2}