How to access/get the size of an array/collection in velocity templates?

52,736

Solution 1

I've never used Velocity, but its VTL reference guide says that calling a method is done using $customer.getAddress() or ${purchase.getTotal()}. So I would use ${myArrayList.size()}.

Solution 2

A collection can be accessed like any other object, so $collection.size() will contain a value.

Arrays are special cased to behave like List, so though $array.length doesn't work, $array.size() works.

In older versions of Velocity (pre 1.6) you'd use ListTool and ArrayTool.

Share:
52,736
Danny
Author by

Danny

Updated on July 11, 2022

Comments

  • Danny
    Danny almost 2 years

    I am using velocity for email templates in my java/spring 3 app.
    How could I get the size of an ArrayList added to the model from within the template.

  • Namphibian
    Namphibian over 9 years
    The reference is one of the funniest pieces of example code I have seen in a while. Had to clean coffee from my screen...