velocity语言之八
来源:互联网 发布:兄弟连细说php视频教程 编辑:程序博客网 时间:2024/06/07 12:34
14.VTL: 一般使用的格式
尽量是这样:
#set( $imperial = ["Munetaka","Koreyasu","Hisakira","Morikune"] )
#foreach( $shogun in $imperial )
$shogun
#end
而不要这样写,虽然它可以运行:
Send #set($foo=["$10 and ","a pie"])#foreach($a in $foo)$a#end please.
Velocity会自动跳过空格,上面的可以这样写::
Send me
#set( $foo = ["$10 and ","a pie"] )
#foreach( $a in $foo )
$a
#end
please.
or as
Send me
#set($foo = ["$10 and ","a pie"])
#foreach ($a in $foo )$a
#end please.
In each case the output will be the same.
15.Other Features and Miscellany(其它特性和细节)
1.数学计算
如下是四则运算的例子:
#set( $foo = $bar + 3 )
#set( $foo = $bar - 4 )
#set( $foo = $bar * 6 )
#set( $foo = $bar / 2 )
2.Range Operator
The range operator can be used in conjunction with #set and #foreach statements. Useful for its ability to produce an object array containing integers, the range operator has the following construction:
[n..m]
Both n and m must either be or produce integers. Whether m is greater than or less than n will not matter; in this case the range will simply count down. Examples showing the use of the range operator as provided below:
First example:
#foreach( $foo in [1..5] )
$foo
#end
Second example:
#foreach( $bar in [2..-2] )
$bar
#end
Third example:
#set( $arr = [0..1] )
#foreach( $i in $arr )
$i
#end
Fourth example:
[1..3]
Produces the following output:
First example:
1 2 3 4 5
Second example:
2 1 0 -1 -2
Third example:
0 1
Fourth example:
[1..3]
Note that the range operator only produces the array when used in conjunction with #set and #foreach directives, as demonstrated in the fourth example.
Web page designers concerned with making tables a standard size, but where some will not have enough data to fill the table, will find the range operator particularly useful.
3.Advanced Issues: Escaping and !
When a reference is silenced with the ! character and the ! character preceded by an \ escape character, the reference is handled in a special way. Note the differences between regular escaping, and the special case where \ precedes ! follows it:
#set( $foo = "bar" )
$\!foo
$\!{foo}
$\\!foo
$\\\!foo
This renders as:
$!foo
$!{foo}
$\!foo
$\\!foo
Contrast this with regular escaping, where \ precedes $:
\$foo
\$!foo
\$!{foo}
\\$!{foo}
This renders as:
$foo
$!foo
$!{foo}
\bar
4.Velocimacro Miscellany(关于宏的一些问题)
这是一些简短的问题总结,也许你先要有这样一个概念:. 'Velocimacro' 就像一个‘VM’。
可否用一个指示符做为另外一个指示符运算的参数?
如 : #center( #bold("hello") )
No. 指示符不是有效的参数但你可以这样实现你想要的:
#set($stuff = "#bold('hello')" )
#center( $stuff )
或者:
#center( "#bold( 'hello' )" )
上面这个例子中,参数是在调用的宏中生成的.不是调用者传入的. 看看下面的例子 :
#macro( inner $foo )
inner : $foo
#end
#macro( outer $foo )
#set($bar = "outerlala")
outer : $foo
#end
#set($bar = 'calltimelala')
#outer( "#inner($bar)" )
输出将是:
Outer : inner : outerlala
因为 "#inner($bar)" 是发生在 #outer()内部的!
可否通过 #parse()来注册一个宏 ?
宏必须在模板使用前定义好.前面己有一个关于此问题的建议,#parse()是运行时执进的,JVM查找对象的顺序不一定会全按我们预计的执行。
什么是宏的自动重新装载?
这是由配置参数决定的, 为方例开发者,在生产环境中则不需要 :
velocimacro.library.autoreload
默认的是false.当设为true中,需要设定chcheing参数;
<type>.resource.loader.cache = false
(具体配置请见开发指南,如下是一个配置的例子)
file.resource.loader.path = templates
file.resource.loader.cache = false
velocimacro.library.autoreload = true
注意:Don't keep this on in production.
5.String Concatenation(连结字符串)
很简单,看例子就是 :
#set( $size = "Big" )
#set( $name = "Ben" )
The clock is $size$name.
上面的输出将是
'The clock is BigBen'.
或者:
#set( $size = "Big" )
#set( $name = "Ben" )
#set($clock = "$size$name" )
The clock is $clock.
它们都是同样的输出,最后一个例子如下,:
#set( $size = "Big" )
#set( $name = "Ben" )
#set($clock = "${size}Tall$name" )
The clock is $clock.
输出将是
'The clock is BigTallBen'.
- velocity语言之八
- velocity语言之二
- velocity语言之三
- velocity语言之四
- velocity语言之5
- velocity语言之六
- velocity语言之七
- Velocity语言
- Velocity介绍-入门之二 语言本身介绍
- 模板语言:Velocity简介
- velocity语言之一
- 模板语言velocity
- C语言入门教程之八多维数组
- NopCommerce架构分析之八------多语言
- R语言数据分析系列之八
- Golang 语言基础之八: interface
- C语言的那些事儿之八
- Go语言编程(八)之函数
- 什么是CPAN?
- Using Window Properties
- sudo 和 su
- javascript DOM 篇
- C++ 实现 根据字符串 调用同名函数
- velocity语言之八
- 2011-2012,走过的痕迹(1)
- 直接插入排序Linux下c 实现
- iOS开发20:使用Settings Bundle为程序添加设置项
- org.eclipse.ui.console指南
- __attribute__((packed))
- oracle数据库日志文件版本不一致问题处理
- 更新sdk出现ClassNotFoundException
- 修改thinkphp缩略图源码 生成固定的缩略图