开发者

For loop in velocity template

开发者 https://www.devze.com 2023-03-06 17:51 出处:网络
I would like to use the for loop in velocity template like below - for(int i = 0; i < 10; i++){} Any idea how to define in vm?

I would like to use the for loop in velocity template like below -

for(int i = 0; i < 10; i++){}

Any idea how to define in vm?

T开发者_如何学Pythonhanks in advance


Range Operator:

#foreach($i in [0..9])
    $i
#end


Adding to serg's answer, if you want a zero-indexed loop but only have an exclusive end value (and don't want to subtract 1 with #set), you can 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.

Let's say $n is 3.

We start the range at 1 as it's inclusive, and so it will loop with $unused being [1, 2, 3, 4, 5], whereas $foreach.index will be [0, 1, 2, 3, 4].

See the user guide for more.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号