API / Apache Velocity Interview questions
How do you render array or list elements with #foreach?
#foreach iterates any Java array, Collection, or Iterator placed into the context, binding each element to a loop variable.
#foreach($product in $products) $velocityCount: $product.name - $$product.price #end
The built-in $velocityCount reference (configurable, 1-based by default) gives the current iteration number without you having to maintain a counter manually. In Velocity 2.x, the richer $foreach object is also available inside the loop, exposing $foreach.index (0-based), $foreach.count (1-based), $foreach.hasNext, and $foreach.first/$foreach.last booleans for more control over formatting, like adding separators between items.
More Related questions...