Get count of items in a velocity list
Easy: $chunk.getSiblings().size()
How to find out? It's a plain old Java object (java.util.ArrayList in my quick test). You can find this out when you just temporarily debug your template with $chunk.getSiblings().getClass().getName()
and then continue with the interface of that class.
Related videos on Youtube
![Tom](https://i.stack.imgur.com/Q8MQn.png?s=256&g=1)
Tom
Updated on September 15, 2022Comments
-
Tom almost 2 years
I'm creating a set of custom templates and structures for a Liferay site.
One structure provides for a repeatable section, which its matching template then iterates over.
However, for styling reasons, I need to know how many instances of the repeatable section are actually present, and I need to know before I loop.
So, the template code is something like this:
#foreach($thisChunk in $chunk.getSiblings()) [emit some HTML] #end
I want to do some conditional logic before the foreach, and emit a different CSS classname on the containing element depending on how many $chunks there are.
Any ideas how to access the number of siblings without looping through them first?
-
Tom almost 12 yearsI should have known (I tried .size & .getSize()). Not a java dev myself. Thanks! Also, that extra debugging tip is solid gold. You DO rule.