API / Apache Velocity Interview questions
What are the types of loops available in Velocity?
VTL has a single loop directive, #foreach, but it can iterate over several kinds of sources.
| Source | Example |
| Java Collection or array | #foreach($item in $items) |
| Map (iterating values) | #foreach($v in $map.values()) |
| Map entries | #foreach($entry in $map.entrySet()) |
| Numeric range | #foreach($i in [1..5]) |
Unlike Java, there's no classic counting for or while loop in VTL — everything goes through #foreach. Inside the loop, the built-in $velocityCount variable (1-based by default) or $foreach.index (0-based, in Velocity 2.x) tracks the current position.
More Related questions...