Monday, October 12, 2020

Apache Velocity : How to define a custom method like in Java

 

Maybe you are looking for a macro ? http://people.apache.org/~henning/velocity/html/ch07.html

Snippet from the above link:

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

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

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

The tablerows macro takes exactly two arguments. The first argument takes the place of $color, and the second argument takes the place of $values. Anything that can be put into a VTL template can go into the body of a Velocimacro.

Notice that $greatlakes takes the place of $values. When this template is rendered, the following output is generated:

<table>
<tr><td bgcolor="blue">Superior</td></tr>
<tr><td bgcolor="blue">Michigan</td></tr>
<tr><td bgcolor="blue">Huron</td></tr>
<tr><td bgcolor="blue">Erie</td></tr>
<tr><td bgcolor="blue">Ontario</td></tr>
</table>


Reference :


Previous Post
Next Post

0 komentar: