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
1 个解决方案
#1
As of Velocity 1.6, optional or named parameters are not supported. There was a recent patch submitted with this feature so we might see it available in a future release.
从Velocity 1.6开始,不支持可选参数或命名参数。最近使用此功能提交了一个补丁,因此我们可能会在将来的版本中看到它。
In the meantime, consider passing in a list or a map of values. For example you can pass in a map of params as follows (requires Velocity 1.5 or greater):
在此期间,请考虑传入列表或值映射。例如,您可以按如下方式传递参数图(需要Velocity 1.5或更高版本):
#macro(myMacro $p)
item 1: $p.param1
item 2: $p.param2
#end
#set($params = {"param1" : "val1", "param2":"val2"})
#myMacro($params)
displays:
item 1: val1
item 2: val2
To handle optional parameters, use an #if within the macro to check for the parameter. Adding new elements to the map is a little messy. Since the Java method "put" returns a value, you have to use #set to dispose of the return value. (Otherwise it's displayed in the resulting text).
要处理可选参数,请在宏中使用#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)
displays
item 1: val1
item 2: val2
item 3: default3
#1
As of Velocity 1.6, optional or named parameters are not supported. There was a recent patch submitted with this feature so we might see it available in a future release.
从Velocity 1.6开始,不支持可选参数或命名参数。最近使用此功能提交了一个补丁,因此我们可能会在将来的版本中看到它。
In the meantime, consider passing in a list or a map of values. For example you can pass in a map of params as follows (requires Velocity 1.5 or greater):
在此期间,请考虑传入列表或值映射。例如,您可以按如下方式传递参数图(需要Velocity 1.5或更高版本):
#macro(myMacro $p)
item 1: $p.param1
item 2: $p.param2
#end
#set($params = {"param1" : "val1", "param2":"val2"})
#myMacro($params)
displays:
item 1: val1
item 2: val2
To handle optional parameters, use an #if within the macro to check for the parameter. Adding new elements to the map is a little messy. Since the Java method "put" returns a value, you have to use #set to dispose of the return value. (Otherwise it's displayed in the resulting text).
要处理可选参数,请在宏中使用#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)
displays
item 1: val1
item 2: val2
item 3: default3