Velocity宏定义的坑与解决办法
来源:互联网 发布:麦萌网络 编辑:程序博客网 时间:2024/05/23 01:25
Velocity宏定义的坑与解决办法
使用Velocity,当然就免不了要使用宏,或者说使用Velocity而不使用其宏,就相当于废了Velocity一半以上的武功,非常可惜的。
怎么使用Velocity的宏呢,才最大程度的发挥其作用但是又避免掉入其中的坑呢?且听悠然乱弹乱弹:
官方文档中,关于Macro是这么说的:
#macro - Allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required
Format:
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #end [ } ]
- vmname - Name used to call the VM (#vmname)
- $arg1 $arg2 [ ... ] - Arguments to the VM. There can be any number of arguments, but the number used at invocation must match the number specified in the definition.
- [ VM VTL code... ] - Any valid VTL code, anything you can put into a template, can be put into a VM.
Once defined, the VM is used like any other VTL directive in a template.
#vmname( $arg1 $arg2 )
当然,上面清晰的说明了怎么写Macro,
也就是说可以写成:
1
#
{macro}(macroName $varName
1
$varName
2
)
2
3
##
这里是模板内容
4
5
#
end
1
#macro(macroName $varName1 $varName2)
2
3
##这里是模板内容
4
5
#end
OK,确实很简单,但是上面的说法实际上只解决了如何写出满足正确语法的宏,但是实际应用当中,如果不加以约束,可能就出现非常难以查找的问题。
比如下面定义了一个超链接的宏,由于id不是每次都用得到,因此id是可选参数,可以填,也可以不填:
1
#macro(link $href $id)
2
<
a
href
=
"$!href"
#if($id)
id
=
"$id"
#end>$bodyContent</
a
>
3
#end
1
#@link("www.tinygroup.com")TinyGroup#end
上面的写法有问题么??似乎没有什么问题,即使什么参数也不传,只写下面的调用方法:
1
#@link()#end
1
<
a
href
=
""
></
a
>
接下来,我们又要写一个图片链接的宏,同样的由于id不是每次都需要,我们把它写成可选的:
1
#macro(image $href $id)
2
<
img
src
=
"$href"
#if($id)
id
=
"$id"
#end>
0 0
- Velocity宏定义的坑与解决办法
- Velocity宏定义的坑与解决办法
- Velocity宏定义的坑与解决办法
- Velocity宏定义的坑与解决办法
- Velocity与jQuery的$发生冲突时的三种解决办法
- velcoity使用说明:velocity宏定义#SLITERAL
- Struts 与 Velocity 的集成
- Struts 与 Velocity 的集成
- Velocity与struts的结合
- Struts 与 Velocity 的集成
- velocity与jsp的区别
- Struts 与 Velocity 的集成
- JSP与Velocity的比较
- FreeMarker与Velocity的区别
- Struts 与 Velocity 的集成
- velocity 宏的使用
- libcef函数名与windowsx.h宏定义冲突的解决办法
- c++ 中类重复定义的分析与解决办法
- 设计模式之创建新模式
- 动态SQL、绑定变量、静态SQL的性能对比
- navicat执行sql文件时,中文乱码的处理
- 在Html<span>添加CSS样式
- 个人知识管理
- Velocity宏定义的坑与解决办法
- ORALCE 的字符串转换为列表
- jQuery Plugin Boilerplate
- UINavigationController 复习知识
- 统计频率【map的用法】【字典序】
- Qt事件过滤
- 第7周作业1-循环大战(DO-WHILE语句)
- 【Spring】IOC核心源码学习:容器初始化过程
- 存储数据のSharedPreferences