Velocity模板语言(VTL):说明

VTL statement

语法部分

directive(指令)
#set( $a = "Velocity" ) // 赋值语句

在上面的例子中,#set是分配一个值给变量.变量$a在模板中输出

  • Velocity中仅有String可以被赋值给变量
  • 使用$字符开始的references用于得到什么;使用#字符开始的directives用于作些什么.
Hello Velocity World!
<html>
  <body>
    #set( $foo = "Velocity" )
    Hello $foo World!
  </body>
</html> 
注释
## This is a sing line comment.

#* Thus begins a multi-line comment. *#

#** This is a VTL comment block and may be used to store such information as the document author and versioning information:
@author
@version *#
References

在VTL中有三种类型References: 变量(variables),属性(properties),方法(methods).

  • 所有的reference被作为一个String对象处理.如果有一个对象$foo是一个Integer对象,那么Velocity将调用它的toString()方法将这个对象转型为String类型.
变量(variables)

非正式变量是由’$’开关,接着是VTL标识符.VTL标识符必须以字母(a-z|A-Z)开关,剩下的部分限于以下几种: (a-z|A-Z|0-9|-|_)

:例: costmer ## 非正式{costmer} ## 正式

属性(properties)

非正式变量是由’$’开头,接着是VTL标识符,再接着就是字符(“.”)和其他的VTL标签符.

:例: costomer.Address ## 非正式{costomer.Address} ## 正式

上面的例子有两种含义.它可以表示:查找hashtable对象customer中以Address为关键字的值;也可以表示调用customer对象的getAddress()方法.当你的页面被请求时,Velocity将确定以上两种方式选用哪种,然后返回适当的值.

方法(methods)

非正式方法是由’$’开始并跟随VTL标识符组成的References,一般还包括一个VTL方法体.一个方法体包括一个VTL标识接着一个左手号(‘(‘),接着是参数列表,再接着是右括号(‘)’).
这里是一些在VTL中有效的方法定义:

:例: customer.getAddress() ## 非正式page.setTitle( “My Home Page!” ) ## 非正式
{customer.getAddress()} ## 正式{customer.setTitle()} ## 正式

Quiet reference notation
<input type="text" name="email" value="$email"/>

当form最初加载的时候,变量 email," email”.使用quiet reference notation可以使Velocity正常显示.你需要用 !email email.所以上面的例子会改成下面

非正式

<input type="text" name="email" value="$!email"/>

正式

<input type="text" name="email" value="$!{email}"/>
Escaping valid VTL reference (逃避特殊符号)
#set( $email = "foo" )
$email
\$email
\\$email
\\\$email

将显示为
foo
$email
\foo
\$email

Case substitution
$foo.getBar()
## is the same as 
$foo.Bar

$data.getUser("jon")
## is the same as 
$data.User("jon)
Directives
#set
#set( $primate = "monkey" )
#set( $customer.Behavior = "$primate" )
  • 左边一定是一个变量或者一个属性
  • 右边可以是(变量,字符串,属性,方法,数字,数组,算术表达式)
  • 如果右边是null,VTL的处理将比较特殊,它将指向一个已经存在的reference
条件语句 (if/elseif/else/end)
#if( $foo && $bar)
  <strong> This and that</strong>
#end
循环 foreach
#foreach( $product in $allProducts )
  <li>$product</li>
#end
  • $allProducts可以 是珍上Vector,Hashtable,Array
  • 默认循环索引变量 $velocityCount可以在velocity.properties设置
include

include允许模板设计者引入本地文件,被引入文件的内容将不会通过模板引擎被render.为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下.

include( “one.gif”, “two.txt”, “three.txt” ) ## 多个文件用’,’分隔

parse

就像#include,#parse接受一个变量而不是模板,任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下.与#include不同的是,#parse只能指定单个对象

#stop

stop 允许模板设计者停止执行模板引擎并返回,把它应用于debug是很有帮助的.

Velocimacros

macro允许定义一段可征用的VTL template.


#macro( d )
  <tr><td></td></tr>
#end

#d()
  • Velocimacro arguments (Reference, String literal, Number literal, IntegerRange[1..3],对象数组,boolean值)
Velocimacro properties
  • velocimacro.library: 一个逗号分隔的模板库列表.默认情况下velocity查找唯一的一个库: VM_global_library.vm.
  • velocimacro.permissions.allow.inline属性:有两个可靠的值true或者false,通过它可以确定Velocimacros是否可以被定义在regular template内.默认值是true.
  • velocimacro.permissions.allow.inline.replace.global属性有两个可靠的值true或者false,通过它可以确定inline的Velocimacro定义是否可以替代全局的Velocimacro定义
  • velocimacro.permissions.allow.inline.local.scale属性有两个可靠的值true或者false,默认false.作用是用于确定inline定义的velocimacros是否仅仅补定义的template内可见
  • velocimacro.context.localscope属性有true和false两个可选值,默认值为false.当设置为true时,任何在Velocimacro内通过#set()对context的修改被认为是针对此velocimacro的本地设置,而不会永久的影响内容.
  • velocimacro.library.autoreload属性控制velocimacro库的自动加载.默认是false,可以配置为开发模式.
附: 开发环境做以下配置,可避免修改VTL重启容器
file.resource.loader.path = templates
file.resource.loader.cache = false
velocimacro.library.autoreload = true
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值