OpenWGA 7.11 - WebTML reference

WebTML tags » foreach

<tml:foreach type="content" >

Purpose:

Iterates over content documents

Description:

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.


Examples:

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.

<ul>

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

</ul>

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">

    ...

  </tml:foreach>

</tml:case>

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();

  list.add(context("name:home").content());

  list.add(context("name:imprint").content());

  list.add(context("name:sitemap").content());

  return list;

</tml:script>


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

  <tml:link/>

</tml:foreach>


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">

  <tml:link/>

</tml:foreach>