What can we loop over in DWT (Layout) Template Building Blocks?
Asked Answered
P

2

7

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 Presentations
  • ComponentTemplate 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}@@?

  • Component1
  • Component2
  • Component3

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 -->
Prewar answered 19/12, 2012 at 7:39 Comment(0)
U
5

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.

Ulric answered 19/12, 2012 at 9:6 Comment(1)
Thanks Bart, I've seen variations of this question lately and wondered how we could somehow "save" the count from whatever creates the package variables. Good stuff.Prewar
B
0

Am unable to create a dummy component Array with tcm:0-0-0. It says item doesnt exist. Is there any other possibility to pass a dummy array through package so that the array can be looped in DWT.

Baram answered 8/2, 2016 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.