I just googled for 'for loop', but it looks like velocity has 'foreach' only.
How do I use 'for loop' in 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.
Approach 1: Using the for loop: The HTML elements can be iterated by using the regular JavaScript for loop. The number of elements to be iterated can be found using the length property. The for loop has three parts, initialization, condition expression, and increment/decrement expression.
Wanted to add that iteration information inside foreach loop can be accessed from special $foreach
property:
#foreach ($foo in $bar) count: $foreach.count index: $foreach.index first: $foreach.first last: $foreach.last #end
(last time I checked last
contained a bug though)
There's only #foreach
. You'll have to put something iterable on your context. E.g. make bar
available that's an array or Collection
of some sort:
#foreach ($foo in $bar) $foo #end
Or if you want to iterate over a number range:
#foreach ($number in [1..34]) $number #end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With