VTL语法

一、引用

1.变量

格式:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

举例:
Normal 格式: $mud-Slinger_9
Silent 格式: $!mud-Slinger_9
Formal 格式: ${mud-Slinger_9}

2.属性

格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]

举例:
Regular 格式: $customer.Address
Formal 格式: ${purchase.Total}

3.方法

格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ opional parameter list... ] ) [ } ]

举例:
Regular 格式: $customer.getAddress()
Formal 格式: ${purchase.getTotal()}
Regular 格式 with Parameter List: $page.setTitle( "My Home Page" )

二、指令

1.#set - 给引用赋值

格式:
#set( $ref = [ ", ' ]arg[ ", ' ] )

用法:
$ref - 左边必须是一个变量引用或者属性引用.
arg - 右边的arg如果被加上双引号则被解析, 如果是单引号则不会解析. 如果右边的值为null,则应 该不会赋值给左边

举例:
变量引用: #set( $monkey = "bill" )
字符串: #set( $monkey.Friend = "monica" )
属性引用: #set( $monkey.Blame = $whitehouse.Leak )
方法引用: #set( $monkey.Plan = $spindoctor.weave($web) )
数字: #set( $monkey.Number = 123 )
范围: #set( $monkey.Numbers = [1..3] )
对象数组: #set( $monkey.Say = ["Not", $my, "fault"] )
右边也可以是简单算术表达式, 例如:

加: #set( $value = $foo + 1 )
减: #set( $value = $bar - 1 )
乘: #set( $value = $foo * $bar )
除: #set( $value = $foo / $bar )
求余: #set( $value = $foo % $bar )

2.#if / #elseif / #else - output conditional on truth of statements

格式:
#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end

用法:
condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
output - May contain VTL.

举例:
Equivalent Operator: #if( $foo == $bar )
Greater Than: #if( $foo > 42 )
Less Than: #if( $foo < 42 )
Greater Than or Equal To: #if( $foo >= 42 )
Less Than or Equal To: #if( $foo <= 42 )
Equals Number: #if( $foo == 42 )
Equals String: #if( $foo == "bar" )
Boolean NOT: #if( !$foo )

3.#foreach - 循环列值

格式:
#foreach( $ref in arg ) statement #end

用法:
$ref - 第一个变量引用.
arg - 可以是object array, collection, or map的引用。
statement -

引用: #foreach ( $item in $items )
数组: #foreach ( $item in ["Not", $my, "fault"] )
范围: #foreach ( $item in [1..3] )

例如:
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>

4. 默认计数器

在velocity.properties文件了有一个默认的循环计数器, 它是$velocityCount. 默认是从1开始计数,但是这个计数器的初值可以在velocity.properties文件里设置. 在velocity.properties如下设置:

# Default name of the loop counter
# refenence refernce.
counter.name = velocityCount

# Default starting value of the loop
# counter 变量引用.
counter.initial.value = 1

5.#include - 不被Velocity解析

格式:
#include( arg[, arg2, ... argn] )

arg -文件名

举例:
字符串: #include( "disclaimer.txt", "opinion.txt" )
变量: #include( $foo, $bar )

6.#parse - 被Velocity解析

格式:
#parse( arg )
arg - 文件名

举例:
字符串: #parse( "lecorbusier.vm" )
变量: #parse( $foo )

7.#stop - 停止模板引擎

格式:
#stop

用法:
将会停止当前模板引擎的执行,这是一个很好的调试。

8.#macro - 定义一个Velocity宏

所有的用户都可以定义一个Velocity宏,允许模板设计者定义一段可重用的VTL template
格式:
#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end
例如:
#macro ( d )
<tr><td></td></tr>
#end
在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它:
#d()
当你的template被调用时,Velocity将用<tr><td></td></tr>替换为#d()。

例如2:带参数的宏
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end
调用#tablerows Velocimacro:
#set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
#set ( $color = “blue” )
<table>
#tablerows( $color $greatlakes )
</table>
经过以上的调用将产生如下的显示结果:
<table>
<tr><td bgcolor=” blue”> Superior </td></tr>
<tr><td bgcolor=” blue”> Michigan </td></tr>
<tr><td bgcolor=” blue”> Huron </td></tr>
<tr><td bgcolor=” blue”> Erie </td></tr>
<tr><td bgcolor=” blue”> Ontario </td></tr>
</table>

三、注释

1. 单行注释 ##

例如:
## This is a comment.

2. 多行注释

Example:
#*
This is a multiline comment.
This is the second line
*#

原文地址:https://www.cnblogs.com/zhuhc/p/3059489.html