OpenWGA 7.9 - WebTML reference

WebTML tags » foreach

<tml:foreach type="itemvalue" >

Purpose:

Iterates over the values stored in an item or WebTML variable

Description:

The type="itemvalue" can be used to iterate over any iterable holding custom objects. Use attribute item to fetch the iterable from an item value of the current context document or a WebTML variable. Use attribute expression to calculate the iterable dynamically. The foreach tag will iterate over the elements of the list and its tag content will get rendered once for each element of it.

The attribute currentvalue specifies the name of a variable which will hold the list value of the current iteration, so you can retrieve it from there inside the <tml:foreach> tag.

If the elements on the iterable are content documents (either in the form of a TMLContext or a WGAPI WGContent object) then the contents of the tag is executed in the context of the currently iterated content document.

Examples:

The following example lists all attachments on the current context document. For that it stores the attachment names (retrieved via WGAPI) into a variable "attachments" which <tml:foreach type="itemvalue"/> iterates over. The name of the current attachment in the iteration is served as variable "attachment" and can be retrieved from inside the foreach body via that name.

<tml:script>
  attachments = content().getFileNames();
</tml:script>


<ul>
  <tml:foreach type="itemvalue" item="attachments" currentvalue="attachment">
    <li><tml:item name="attachment"/>: <tml:script expression="content().getFileSize(attachment)" format="#,##0"/> Bytes
  </tml:foreach>

</ul>

With the new attribute "expression" being available since OpenWGA 7.1 this can be done in even fewer lines of code. Also the "type" attribute can actually be omitted when attribute "item" or "expression" is used:

<ul>
  <tml:foreach expression="content().getFileNames()" currentvalue="attachment">
    <li><tml:item name="attachment"/>: <tml:script expression="content().getFileSize(attachment)" format="#,##0"/> Bytes
  </tml:foreach>

</ul>