The #macro script element allows template designers to define a repeated segment of a VTL template.
A "macro" accept any number of arguments (0..N) that can be used to produce the output.
Macros are not functions, they are for rendering output (not for returning a value).
Examples :
Macro with no arguments :
## ----------------------------------------------------------------------
#macro( three )
#set ( $result = "" )
#foreach ( $i in [1..3] ) ${i}#end
#end
## ----------------------------------------------------------------------
#three() ## No CR here (end of line) if no CR in the macro result
#three
Macro with 2 arguments :
## ----------------------------------------------------------------------
#macro( add $a1 $a2 )
#set ( $r = $a1 + $a2 )
$a1 + $a2 = $r ## rendering = 20 + 3 = 23
#end
## ----------------------------------------------------------------------
#add( 20, 3 )
Macro with collection/array argument :
## ----------------------------------------------------------------------
#macro( buildList $args )
#set ( $count = 0 )
#set ( $list = "" )
#foreach( $a in $args )
#set ( $count = $count + 1 )
#if ( $count > 1 )
#set ( $list = "${list}, " )
#end
#set ( $list = "${list} ${a}" )
#end
$list #end
## ----------------------------------------------------------------------
A to D : #buildList( ['a','b','c','d'] )
Attributes : #buildList( $entity.attributes )
"Function like call" getting the result as text (with quotes)
#set( $list = "#buildList( ['a','b','c','d'] )" )
list = $list