Get count of items in a velocity list

12,467

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.

Share:
12,467

Related videos on Youtube

Tom
Author by

Tom

Updated on September 15, 2022

Comments

  • Tom
    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
    Tom almost 12 years
    I should have known (I tried .size & .getSize()). Not a java dev myself. Thanks! Also, that extra debugging tip is solid gold. You DO rule.