宏的 Velocity 命名参数

发布于 2024-07-13 04:17:26 字数 413 浏览 4 评论 0原文

我有一个带有几个参数的宏。 其中一些是可选的,如果参数留空,它将替换为默认值。

现在的问题是如何让普通网页设计师尽可能轻松地做到这一点。 除了我的例子之外,还有其他可能性来处理这种情况吗?

示例 1:

这里明显的问题是可选值。

#macro (myTag $param1 $param2 $param3)
...
#end

示例 2:

这里的问题是当同一宏多次使用并且所有变量都没有再次设置时可能出现的问题。

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

#macro (myTag)
...
#end

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-07-20 04:17:26

从 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

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.

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):

#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).

#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
信仰 2024-07-20 04:17:26

这是一个 Velocity 宏,它接受两个参数、一个颜色和一个对象列表:

#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>

请在此处查看完整文档: Velocity 替代文档

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

Then you can use the macro in this way: :

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

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

See full documentation here : Velocity alternative documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文