Speed ​​indicated parameters for the macro

I have a macro that takes several parameters. Some of them are optional, and if left blank, it will be replaced by default.

Now the question is how to make this as simple as possible for a regular web designer. Are there other possibilities than 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 problem when the same macro is used more than once and all the variables are not set again.

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

#macro (myTag)
...
#end
+3
source share
1 answer

Velocity 1.6, . , .

. , ( Velocity 1.5 ):

#macro(myMacro $p)
  item 1: $p.param1
  item 2: $p.param2
#end

#set($params = {"param1" : "val1", "param2":"val2"})
#myMacro($params)

:

item 1: val1
item 2: val2

, #if , . . Java "put" , #set . ( ).

#macro(myMacro $p)
  #if(!$p.param1)#set($dummy = $p.put("param1", "default1"))#end
  #if(!$p.param2)#set($dummy = $p.put("param2", "default2"))#end
  #if(!$p.param3)#set($dummy = $p.put("param3", "default3"))#end

  item 1: $p.param1
  item 2: $p.param2
  item 3: $p.param3
#end

#set($params = {"param1" : "val1", "param2":"val2"})
#myMacro($params)

item 1: val1
item 2: val2
item 3: default3
+6

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


All Articles