OpenWGA 7.6 - WebTML reference

WebTML tags » foreach

<tml:foreach type="content" >


Iterates over content documents


This type can be used to iterate over a list of content documents from any source. In iteration it will change the WebTML context of the tag content of <tml:foreach> to the current content document. That way the data of each document can be easily accessed by the known retrieval tags like <tml:item> and <tml:metadata>.

Type "content" is the default type of <tml:foreach> which is assumed if no type attribute is given.

The sources of content documents may be:

  • A <tml:collection> in which <tml:foreach> is contained. It automatically serves the retrieved contents from <tml:query> tags that are contained in the same collection tag.
  • A <tml:navigator/collection> tag that is referenced by attribute sourcetag
  • A <tml:script> tag which returns a list of content documents (WGAPI object WGContent) as result. May also be a list of relations (WGAPI object WGRelationData), where the iteration will use the parent content of the relation (NOT the target content). It is also referenced via attribute sourcetag.


This example iterates over the results of a lucene fulltext query. The <tml:foreach> tag does not need to address the source of content documents as it is contained in the <tml:collection> tag. If it was outside it could reference a <tml:collection> tag to use by its sourcetag attribute.


    <tml:query type="lucene">WGA</tml:query>
    <tml:foreach sortexpression="MODIFIED" sortorder="descending">


The following example references an <tml:navigator> as document source. Note that the navigator tag itself produces no output. This indirect form of navigator output can be useful if you want to output some code only if the navigator actually has results:

<tml:navigator id="newsNav" context="name:news" type="children"/>

<tml:case condition="taginfo('newsNav', 'count') > 0">

  <h2>The News</h2>

  <tml:foreach sourcetag="newsNav">




This example collects custom content documents to iterate over via TMLScript. The list collected is returned as result of a <tml:script> tag (where output="false" prevents the list from getting rendered directly) which is used as content source for <tml:foreach>:

<tml:script id="bottomNavPages" output="false">

  var list = WGA.createList();




  return list;


<tml:foreach sourcetag="bottomNavPages" divider="&nbsp;">



Another example using <tml:script> to collect the data. This time we use the ability of <tml:foreach> to also use relation objects for source. We retrieve relations via method "getIncomingRelations() on WGAPI object WGContent and directly pass the result on to the foreach tag. The result will be an iteration over all contents who are of content class "issue" and have a relation named "targetbuild" pointing to the current context document:

<tml:script id="thedocs" expression="content().getIncomingRelations('issue', 'targetbuild')" output="false"/>

<tml:foreach type="content" sourcetag="thedocs">