Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 661
Danny Avatar asked Oct 12 '11 22:10

Danny


People also ask

How can we find array size?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

Which property contains the size of an array?

Every array has an in-built length property whose value is the size of the array. Size implies the total number of elements that an array can contain. The length property can be invoked by using the dot (.)

What is size array?

The size of an array object is always equal to the second template parameter used to instantiate the array template class (N). Unlike the language operator sizeof, which returns the size in bytes, this member function returns the size of the array in terms of number of elements.

How do I test a Velocity template?

Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.


1 Answers

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()}.

like image 93
JB Nizet Avatar answered Sep 29 '22 18:09

JB Nizet