Mustapha Aoussar
Mustapha Aoussar

Reputation: 5923

How to split a sequence into multiple sequences in Velocity?

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

Answers (1)

Claude Brisson
Claude Brisson

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

Related Questions