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:
Tag Collection variable name Item variable name <tcdl:navigation>sectionssection<tcdl:navigationSection>linkslink<tcdl:promotions>promotionspromotion<tcdl:promotionalItems>promotionalItemspromotionalItem<tcdl:items>itemsitem - Declaring a variable
-
Use the
varattribute in the tag exposing the collection to declare your own variable name, for examplevar='myVariableNameForCollection'. - Calling the item collection
-
Use the
itemsattribute on theitemTemplatetag to call the item collection, for exampleitems='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}
- In Java code, prefix the variable name with a dollar sign, for example
Java example
The following example shows how to use custom variables for Java code:
- In the
<tcdl:navigation>tag, SmartTarget puts a collection of facets relating to the query into thesectionsvariable declared in this tag. - In the first
<tcdl:itemTemplate>tag, which loops over the facet collection,items="#{sections}reads the facet collection and SmartTarget puts the current section into thesectionvariable 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 thelinkvariable 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>