I understand that out-of-the-box we can loop or iterate over arrays in DWT Template Building Blocks (TBBs) with the following.
<!-- TemplateBeginRepeat name="array_name" -->
<!-- template logic -->
<!-- TemplateEndRepeat -->
The documentation describes pre-defined package items as well as how to use them.
In summary we can use:
Component
to reference the current component when iterating over Components or Component PresentationsComponentTemplate
to reference current Component Template when iterating over Component Presentations (this would be at the page-level)Field
to reference current Field when iterating over Fields as well as Multi-Value fields.FieldPath
to get the full path to the iterated item. It's useful when using TemplateRepeatIndex
within a nested loop as seen in The Tridion Practice Cookbook.*TemplateRepeatIndex
, the zero-based loop count.We can do nested loops on Fields
in a given Component within a Components array loop, as well as by conditionally checking a specific field name when iterating over multivalue fields.
I've seen double look-ups by nesting DWT's syntax (e.g. @@Image_${TemplateRepeatIndex}@@
as described on Yet Another Tridion Blog).
Question(s)
To be sure, these are items or arrays/collections in a Compound Template's Package
, right (either there by default or placed by our code)?
Could we possibly "iterate" over separate items in a package?
For example if we have individual components in the Package
, could we "iterate" over them with something like @@Component${TemplateRepeatIndex}@@
?
I see I can reference such an item within a loop, but everything I've seen suggests the following is not possible:
<!-- TemplateBeginRepeat name="@@Component${TemplateRepeatIndex}@@" -->
<!-- do something with @@Component${TemplateRepeatIndex}@@ -->
<!-- TemplateEndRepeat -->
Basically a DWT Template allows you to iterate over arrays and in the Package there is only one sort of array we can push ourselves, which is the Component Presentation array (which can contain Component Template TCMURIs, but also only Component TCMURIs).
Through this you can push (what I refer to as) a Dummy Component Array in the package which you can use to loop over and then through the double lookup feature, to make it work like looping over a set of Package variables.
For example, you push in the package a Component Array with three dummy TCMURIs (they can all be tcm:0-0-0, it doesn't matter what value they have). Then you create three package variables named: Var_0, Var_1, Var_2. Now in your DWT Template you can use:
<!-- TemplateBeginRepeat name="MyDummyArray" -->
@@Var_${TemplateRepeatIndex}@@
<!-- TemplateEndRepeat -->
Which will result you in the values of your three package variables.
Unfortunately there is no way to create a string array directly or a field array for that matter, so for everything you will want to loop over, you will have to use an existing field array, or create a "Dummy Component Array".
From which we indeed can conclude that your example is not possible as written, but when you push a dummy array with them in the Package too, you can loop over that and have what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With