I am need to create 2 loops using TemplateBeginRepeat
where I need to output custom tags
<!-- TemplateBeginRepeat name="customtag" -->
${RenderComponentPresentation(Field, rendercustomtagstarttemplate)}
<!-- TemplateEndRepeat -->
Output some html
<!-- TemplateBeginRepeat name="customtag" -->
${RenderComponentPresentation(Field, rendercustomtagclosetemplate)}
<!-- TemplateEndRepeat -->
Since the second loop closes the custom tag rendered by the first loop, the second loop should run in reverse order. (since tags need to be closed in reverse order). How do i run the second loop in reverse order of components with TemplateBeginRepeat?
There is no built-in way to loop over the repeated items in a reverse order.
If your customtag
is an array item (typically an array of Components or Component Presentations) in the package, you can push a list into the package that contains the same items in reverse order and then loop over that item.
<!-- TemplateBeginRepeat name="customtag_reversed" -->
If your customtag
is a field, this will not work, since you cannot push a field into the package. In that case I suggest creating a custom function that outputs your custom tags in the correct order, e.g.:
@@RenderCustomTags('customtag', 'Order.Reverse')@@
Update
If customtag
is a Component Link field, it's better to simply add those linked Components to the package as a Component Array item. Nuno provided a link to a TBB on SDL Tridion World, but this is the most critical fragment:
// Tridion.ContentManager.Templating.ComponentPresentation
var list = new List<ComponentPresentation>();
list.Add(new ComponentPresentation(Component.Id, ComponentTemplate.Id));
// you'll want to do a loop of these for every linked Component
var item = package.CreateStringItem(ContentType.ComponentArray,
ComponentPresentationList.ToXml(list));
package.PushItem("customtag_Components", item);
You'll want to do a loop of these for every linked Component:
list.Add(new ComponentPresentation(Component.Id, ComponentTemplate.Id));
And instead of hard-coding the Component Template ID in the C# code, you can also consider leaving it empty in C# and keeping it inside the RenderComponentPresentation
call in your DWT like you already do.
The problem here seems to be that Dreamweaver syntax is only suitable for the simplest programming tasks. Frank and Nuno have shown that moving some of the logic to a C# template gives an improvement, but you should also consider moving the generation of this output entirely to the C# template. In other words, once you get to the point of needing to use something other than DWT, your problem definition changes, because the problem as now described is very DWT-centric.
The need to loop in reverse only exists because you want to close your constructs in the correct order. In a language like C# you could achieve this result by using nested (even recursive) function calls, or (perhaps more likely) by pushing your closing output to a stack.
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