Documentation Center

Iterating over collections

When Java code is generated from TCDL variables are automatically created using default names. You can specify your own variable names and use these variables in your TCDL tags, for example if the default variable names conflict with existing names.

Default variable names
By default, the following variable names are used for collections and items in the collections:
TagCollection variable nameItem variable name
<tcdl:navigation>sectionssection
<tcdl:navigationSection>linkslink
<tcdl:promotions>promotionspromotion
<tcdl:promotionalItems>promotionalItemspromotionalItem
<tcdl:items>itemsitem
Declaring a variable
Use the var attribute in the tag exposing the collection to declare your own variable name, for example var='myVariableNameForCollection'.
Calling the item collection
Use the items attribute on the itemTemplate tag to call the item collection, for example items='myVariableNameForCollection':
  • In Java code, prefix the variable name with a dollar sign, for example ${items}
  • In Dreamweaver Templates, prefix the variable name with a hash sign, for example #{VariableName}

Java example

The following example shows how to use custom variables for Java code:
  • In the <tcdl:navigation> tag, Experience Optimization puts a collection of facets relating to the query into the sections variable declared in this tag.
  • In the first <tcdl:itemTemplate> tag, which loops over the facet collection, items="#{sections} reads the facet collection and Experience Optimization puts the current section into the section variable declared in this tag.
  • In the second <tcdl:itemTemplate> tag, which loops over the item collection of the current facet, items="#{section.links} reads the links in the current section and puts them in the link variable declared in this tag.
<tcdl:navigation var="sections" showCategories="true">
	 <tcdl:itemTemplate var="section" items="#{sections}">
    	<tcdl:navigationSection>
	     		<tcdl:navigationSectionHeader>
               <h4>##title##</h4>
     			</tcdl:navigationSectionHeader>
    			 <tcdl:itemTemplate var="link" items="#{section.links}">
  					   <tcdl:navigationLink link="#{link}" /> 
  					 </tcdl:itemTemplate>
   	    <tcdl:navigationSectionFooter />
     </tcdl:navigationSection>
  </tcdl:itemTemplate>
</tcdl:navigation>