Apache Velocity Macro Parameter Values

I want to update a macro that has been split between several different .vtl files to include a new option. However, I only want to change the call in one of my .vtl files and leave the rest unchanged. Therefore, I want to add an optional parameter or parameter with a default value if the parameter is not specified.

The documentation ( here ) mentions the optional default values ​​in the "#macro" section, but after a couple of hours of fijing with them, I can't figure out what the correct syntax is for life.

So, I want to take an existing macro:

#macro( my_macro ) oldValue #end 

And turn it into a macro like:

  #macro( my_macro $param="oldValue" ) $param #end 

Where I could call it correctly using any of these two calls and get the indicated outputs:

  #my_macro() => oldValue #my_macro("newValue") => newValue 

I tried every permutation of what the documentation says, but cannot find anything that works. Does anyone know the correct syntax? Perhaps there is a property that I am missing?

I am using Velocity 1.7 and VelocityTools 2.0. I also use the setting velocimacro.arguments.strict=true if that matters. However, I cannot easily change this property without an update / retest heap.

+5
source share
1 answer

The default setting does not work. But you can do this:

 #macro(my_macro $param) #if(!$param) #set($param = "oldValue") #end $param #end 
+1
source

Source: https://habr.com/ru/post/1214271/


All Articles