Search code examples
velocity

Velocity - Loop through properties of placeholders


I want to loop through a list of properties provided by a placeholder.

#foreach( $property in $placeholder )
    $property
#end

Is there any possible way to access them in my template?

Thanks in advance!


Solution

  • It depends upon your $placeholder object.

    If it's a java.util.Map, you're free to iterate its keys, values or both:

    #foreach($key in $placeholder.keySet())
      property $key is $placeholder[$key]
    #end
    
    #foreach($value in $placeholder.values())
      found value $value
    #end
    
    #foreach($entry in $placeholder.entrySet())
      property $entry.key is $entry.value
    #end
    

    If it's an array or a list, the syntax you've given would work.

    Remember that you can call any java public method on your object. If you're unsure about its class, you can display it with $placeholder.class.name.