Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'for' loop in velocity template?

I just googled for 'for loop', but it looks like velocity has 'foreach' only.

How do I use 'for loop' in velocity template?

like image 215
Moon Avatar asked Apr 16 '11 00:04

Moon


People also ask

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.

Can I use for loop in HTML?

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.


2 Answers

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)

like image 198
serg Avatar answered Sep 21 '22 17:09

serg


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 
like image 24
WhiteFang34 Avatar answered Sep 19 '22 17:09

WhiteFang34