如何在Velocity中将序列拆分成多个序列?

时间:2015-02-16 23:12:20

标签: apache foreach velocity

我有这个数组:

seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

我想将这个序列分成两个包含四个元素的序列,如下所示:

a b c d 
e f g h

我尝试使用两个 foreach 拆分列表,一个用于对四个元素进行分组,另一个用于获取元素。但它不起作用:

#foreach( $groupOfFour in $allProducts )
    #if( $velocityCount == 4 )
        <ul>
        #foreach( $product in $groupOfFour )
            <ul>
                <li>$product</li>
            </ul>
        </ul>
    #end
#end

1 个答案:

答案 0 :(得分:2)

这看起来非常接近question 28420066

在Velocity 1.7+中,使用$ foreach.index(从0开始),$ foreach.count(从1开始),$ foreach.first和$ foreach.last(check the doc)。

<ul>
#foreach( $product in $allProducts )
    #if( $foreach.index %4 == 0 )
        #if( !$foreach.first )
          </ul>
        #end
        <ul>
    #end
    $product
    #if( $foreach.last )
        </ul>
    #end
#end
</ul>