Reputation: 5923
I have this array:
seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
I want to split this sequence into two sequences with four elements, like this:
a b c d
e f g h
I tried to split the list using two foreach, one for grouping the four elements and the other to get elements. But it doesn't work:
#foreach( $groupOfFour in $allProducts )
#if( $velocityCount == 4 )
<ul>
#foreach( $product in $groupOfFour )
<ul>
<li>$product</li>
</ul>
</ul>
#end
#end
Upvotes: 0
Views: 118
Reputation: 4130
This looks like very close to question 28420066.
In Velocity 1.7+, use $foreach.index (0-based), $foreach.count (1-based), $foreach.first and $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>
Upvotes: 2