'Velocity named parameters for a macro

I have a macro taking several parameters. Some of these are optional and if a parameter is left empty it will replaced with default.

Now the question is how to make this as easy as possible for ordinary web designer. Is there any other possibity apart from my examples to handle this case?

Example 1:

The obvious problem here is the optional values.

#macro (myTag $param1 $param2 $param3)
...
#end

Example 2:

And here the problem is a possible issue when same macro is used more than once and all variables are not set again.

#set ($param1="value1") 
#set ($param2="value2") 
#set ($param3="value3") 

#macro (myTag)
...
#end


Solution 1:[1]

Here is a Velocity macro that takes two arguments, a color and a list of objects:

#macro( tablerows $color $values )
  #foreach( $value in $values )
    <tr><td bgcolor=$color>$value</td></tr>
  #end
#end

Then you can use the macro in this way: :

#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#set( $color = "blue" )

<table>
  #tablerows( $color $greatlakes )
</table>

See full documentation here : Velocity alternative documentation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A.Casanova