stracktracer
stracktracer

Reputation: 1900

Velocity code generation: check whether $foreach is entered?

I try to modify a vsl file for code generation to check whether a foreach loop is actually entered and the initialization code is to be performed at all. I tried java-like .length > 0 but that does not work:

myMethod() {
    #if (${reference.attributes}.length > 0)
        [some init code]

        #foreach ($attribute in ${reference.attributes})
            #some_macro($attribute)
        #end
        [some other code related to stuff created in the 
    #end
}

What's the correct way?

Thanks.

Upvotes: 0

Views: 290

Answers (2)

Dave Newton
Dave Newton

Reputation: 160291

Call the size() method. This will work on both collections and arrays (as of Velocity 1.6+).

#if (${reference.attributes.size()} > 0)

Upvotes: 4

Rupok
Rupok

Reputation: 2082


#foreach ($foo in $bar)
    $foo
#end

Or if you want to iterate over a number range:

#foreach ($number in [1..34])
    $number
#end

Upvotes: -1

Related Questions