Reputation: 23352
I want to use for loop in a Velocity view. I want to take a counter variable in velocity view and loop till it equals. e.g
counter = 3
for(i=0; i< counter; i++){
...
}
Upvotes: 1
Views: 2068
Reputation: 2417
Alex's answer works fine, but do note it loops 4 times total because the start and end are inclusive in a Velocity #foreach
loop. Another way to do it if you want a zero-indexed loop and have an exclusive end value, is to use the builtin $foreach.index
. If you want to loop $n
times:
#foreach($unused in [1..$n])
zero indexed: $foreach.index
#end
here, $unused
is unused, and we instead use $foreach.index
for our index, which starts at 0.
In the question's case, $n
is 3.
We start the range at 1 as it's inclusive, and so it will loop with $unused
being [1, 2, 3], whereas $foreach.index
will be [0, 1, 2].
See the user guide for more.
Upvotes: 2
Reputation: 25613
You can use foreach
for this, by defining a range and iterating over it.
#set($start = 0)
#set($end = 3)
#foreach($i in [$start..$end])
...
#end
Upvotes: 10