Nested loops and the TemplateRepeatIndex and FieldPath variables
When processing multivalue embedded fields (among others), your Dreamweaver templating code contains one iterative loop inside another. The problem addressed here is that your inner loop is unable to access the outer iterator.
This problem occurs because each of the loops has a TemplateRepeatIndex variable, referring to the iterator in that loop. The solution is the use of a variable called FieldPath, which eliminates the need to access the outer iterator.
For example, imagine a Component with a multivalue field called body, where body is itself an embedded Field that contains another multivalue field called section. The following is an example Component XML for such a Component:
<Content xmlns="uuid:8841d68e-7b1b-45cd-a6d6-7e7da5de3ef9">
<body>
<section>Blue section</section>
<section>Green section</section>
</body>
<body>
<section>Yellow section</section>
<section>Red section</section>
</body>
</Content>
To produce output for all the <section> elements, create a nested loop that uses the FieldPath variable:
@@FieldStartMarker("body")@@
<!-- TemplateBeginRepeat name="body" -->
@@FieldValueStartMarker(TemplateRepeatIndex)@@
@@FieldStartMarker("Field.section")@@
<!-- TemplateBeginRepeat name="Field.section" -->
@@FieldValueStartMarker(TemplateRepeatIndex)@@
<div>
@@GetFieldValue("Field.section", TemplateRepeatIndex)@@
</div>
@@FieldValueEndMarker()@@
<!-- TemplateEndRepeat -->
@@FieldEndMarker()@@
@@FieldValueEndMarker()@@
<!-- TemplateEndRepeat -->
In this code example:
- The outer loop iterates over the values of the outer multivalue field called
body. - The inner loop iterates over the values of the inner multivalue field called
section. - The
Fieldvariable inField.sectionrefers to the current value of thebodymultivalue field. - Within the inner loop,
FieldPathrefers to the specific value ofbodynow being examined:body[0]orbody[1]. - Within the inner loop,
TemplateRepeatIndexrefers to the iterator of the inner loop, that is, the index number of the current value ofsectionnow being examined: 1 or 2.
When we apply this template code to the input shown above, the output is as follows:
<div>
<tcdl:Field xPath="tcm:Content/custom:Content/body[0]/section" itemId="tcm:1-109">
<tcdl:FieldValue index="0">Blue section</tcdl:FieldValue>
</tcdl:Field>
</div>
<div>
<tcdl:Field xPath="tcm:Content/custom:Content/body[0]/section" itemId="tcm:1-109">
<tcdl:FieldValue index="1">Green section</tcdl:FieldValue>
</tcdl:Field>
</div>
<div>
<tcdl:Field xPath="tcm:Content/custom:Content/body[1]/section" itemId="tcm:1-109">
<tcdl:FieldValue index="0">Yellow section</tcdl:FieldValue>
</tcdl:Field>
</div>
<div>
<tcdl:Field xPath="tcm:Content/custom:Content/body[1]/section" itemId="tcm:1-109">
<tcdl:FieldValue index="1">Red section</tcdl:FieldValue>
</tcdl:Field>
</div>
For extra clarity, while developing your template code, be sure to output the values of FieldPath and TemplateRepeatIndex at the top of every loop with a statement such as:
(FieldPath=@@FieldPath@@, TemplateRepeatIndex=@@TemplateRepeatIndex@@)