How to get the Maximum index count for TemplateRepeatIndex in DWT
Asked Answered
V

4

6

I have a component in tridion where its metadata design has a field called 'list' which is populated using Categories and Keywords

I used a DWT code to populate items present in the list using the following code using My DWT TBB

<!-- TemplateBeginRepeat name="Metadata.list" -->

    <!-- TemplateBeginIf cond="list" --> 
        @@RenderComponentField('list',TemplateRepeatIndex)@@ ,
    <!-- TemplateEndIf -->

<!-- TemplateEndRepeat -->

but Im getting preview as

one,two,three,four,five,

desired output should be like: one,two,three,four,five

so for this i need to get the maximum count of "TemplateRepeatIndex" Is there any inbuilt function to check the same.

using Tridion-sp1,2011.

Vane answered 21/8, 2012 at 9:44 Comment(0)
M
9

You can solve this with the built in function: CollectionLength.

When you have a multi-valued text field "multiValuedField" you can find the item count using the following expression:

@@CollectionLength("Component.Fields.multiValuedField")@@

Collection Length receives an expression that is the fully qualified name of an item and a value selector of a package item.

The value returned is a string. When you need to perform an integer comparison or calculation you need to parse the value to an integer:

@@parseInt(${CollectionLength("multivalued")})@@

This works because the string between @@ and ${} is parsed as JScript.

Mera answered 21/8, 2012 at 12:24 Comment(9)
its not a multivalued feild.Its a text field, with list of type 'Selection Box'Vane
isn't it still a multi-valued text field? You have to select a text field, and then a list type and allow multiple values.Mera
the CollectionLength is not working for the above field type.Vane
I have run a test with a multi-value text field, with list type set to Select Box, and it seems to work just fine. <xsd:element name="muliValuedField" minOccurs="1" maxOccurs="unbounded" type="category:age"> <xsd:annotation> <xsd:appinfo> <tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml> <tcm:Size xmlns:tcm="http://www.tridion.com/ContentManager/5.0">5</tcm:Size> <tcm:listtype xmlns:tcm="http://www.tridion.com/ContentManager/5.0">select</tcm:listtype> </xsd:appinfo> </xsd:annotation> </xsd:element>Mera
<!-- TemplateBeginRepeat name="multivalued" --> <!-- TemplateBeginIf cond = "TemplateRepeatIndex=@@CollectionLength("multivalued")@@-1" --> <li> @@RenderComponentField("multivalued", TemplateRepeatIndex)@@ </li> <!-- TemplateEndIf --> <!-- TemplateEndRepeat -->Vane
i'm unable to get the last item in the listVane
@@CollectionLength("multivalued")@@ will return a string and if we subtract 1 from that string in DWT it is taking the whole as a string instead of performing subtraction,this might be the problem.How can we overcome this?Vane
This would do it: @@parseInt(${CollectionLength("multivalued")})@@ This works because the string between @@ and ${} is parsed as JScript.Mera
@Arjen: it might make sense to update your answer with that last snippet, so it's a bit easier to find for those that stumble upon this later.Holzer
S
6

An easy solution would be to switch your logic around.

You can check if TemplateRepeatIndex is 0. If it is not, output the comma in front of the value.

Shallot answered 21/8, 2012 at 10:42 Comment(2)
Of course, it's obvious now you mention it - I think I've even used that approach before...Wame
yup, thats the logic i'm currently using.Thanks.Vane
W
2

I believe the only way to do this would be to create a TBB that adds the field count as an item in the package. You would then be able to do the following:

<!-- TemplateBeginRepeat name="Metadata.primary_topic" -->

<!-- TemplateBeginIf cond="primary_topic" --> 
@@RenderComponentField('primary_topic',TemplateRepeatIndex)@@<!-- TemplateBeginIf cond="TemplateRepeatIndex &lt; PrimaryTopicCount"--> ,<!-- TemplateEndIf -->
<!-- TemplateEndIf -->

<!-- TemplateEndRepeat -->
Wame answered 21/8, 2012 at 9:54 Comment(2)
you mean a C# TBB. Can we take a loop in which we set a variable to count the repeatIndex and then after the end of the loop declaring another loop to get the value from the variable which gives the maximum count.Vane
A C# Fragment or Assembly that counts the number of primary_topic values and pushes this count value to the package.Mera
H
2

Walter solved and explained this topic a few years ago by introducing a custom function.

http://sdltridionworld.com/community/extension_overview/dreamweavercia.aspx

Don't forget to recompile his code for the current version of Tridion.

Update: you can find all built-in functions (and examples of their usage) that Tridion has for use in DWT on this page: http://code.google.com/p/tridion-practice/wiki/BuiltInFunctionsForUseInHtmlTemplates

Holzer answered 21/8, 2012 at 11:21 Comment(2)
so without defining any custom functions, we cant get the maximum value count for TemplateRepeatIndex.Any poniters to inbuilt DWT functions?.Vane
code.google.com/p/tridion-practice/wiki/…Holzer

© 2022 - 2024 — McMap. All rights reserved.