Velocity教程_velocity定义集合变量

156 阅读1分钟

$velocityCount

This is $element.
#end

输出的结果为:

1 This is pine.

2 This is oak.

3 This is maple.

每次循环list中的一个值都会赋给list中的一个值都会赋给element变量。

list可以是一个VectorHashtable或者Array。分配给list可以是一个Vector、Hashtable或者Array。分配给element的值是一个java对象,并且可以通过变量被引用。

例如:如果$element t是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。

#foreach ( keyinkey in list.keySet())

Key: key>Value:key -> Value: list.get($key)

#end

提示:velocity中大小写敏感。

Velocity还特别提供了得到循环次数的方法,$velocityCount变量的名字是Velocity默认的名字。

 例子:

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 ( iini in arr )

    $i

   #end

上面三个例子的输出结果为:

 First example:

 1 2 3 4 5

Second example:

  2 1 0 -1 -2

Third example:

 0 1

3. 条件语句

#if (condition)

#elseif (condition)

#else

 #end

4. 语句的嵌套

#foreach (elementinelement in list)

 ## inner foreach 内循环

#foreach (elementinelement in list)

This is element.element. velocityCount
inner

#end

 ## inner foreach 内循环结束

 ## outer foreach

This is $element.

$velocityCount
outer

#end

语句中也可以嵌套其他的语句,如#if…#else…#end等。

5. 注释

(1)单行注释:

 ## This is a single line comment.

(2)多行注释:

#*

 Thus begins a multi-line comment. Online visitors won’t

 see this text because the Velocity Templating Engine will

 ignore it.

 *#(3)文档格式:

#**

 This is a VTL comment block and

 may be used to store such information

 as the document author and versioning

information:

@version 1.1

@author xiao

*#

6. 关系和逻辑操作符

Velocity 也具有逻辑AND, OR 和 NOT 操作符。

example for AND

#if(foo && bar)This and that

#end

例子中#if() 指令仅在foofoo 和bar 斗为真的时候才为真。如果$foo 为假,则表达式也为假;

并且 bar将不被求值。如果bar 将不被求值。如果 foo 为真,Velocity 模板引擎将继续检查bar的值,如果bar的值,如果 bar

 为真,则整个表达式为真。并且输出This AND that 。如果 $bar 为假,将没有输出因为整

个表达式为假。

7.Velocity 中的宏

Velocity中的宏我们可以理解为函数。

①宏的定义

#macro(宏的名称 参数1参数1 参数2 …)

       语句体(即函数体)

#end

②宏的调用

      #宏的名称(参数1参数1 参数2 …)

                说明:参数之间用空格隔开。

8.#stop

 停止执行模板引擎并返回,把它应用于debug是很有帮助的。

9.#include与#parse

#include和#parse的作用都是引入本地文件, 为了安全的原因,被引入的本地文件只能在

TEMPLATE_ROOT目录下。

区别:

(1) 与#include不同的是,#parse只能指定单个对象。而#include可以有多个

如果您需要引入多个文件,可以用逗号分隔就行:

#include ("one.gif", "two.txt", "three.htm" )

在括号内可以是文件名,但是更多的时候是使用变量的:

#include ( “greetings.txt”, $seasonalstock )

(2) #include被引入文件的内容将不会通过模板引擎解析;

 而#parse引入的文件内容Velocity将解析其中的velocity语法并移交给模板,意思就是说

相当与把引入的文件copy到文件中。

#parse是可以递归调用的,例如:如果dofoo.vm包含如下行:

Count down.

#set ($count = 8)

#parse ("parsefoo.vm")

All done with dofoo.vm!

那么在parsefoo.vm模板中,你可以包含如下VTL:

$count

#set(count=count = count - 1)

#if ( $count > 0 )

#parse( "parsefoo.vm" )

#else

All done with parsefoo.vm!

#end的显示结果为:

Count down.

8

7

6

5

4

3

2

1

0

All done with parsefoo.vm!

All done with dofoo.vm!

注意:在vm中使用#parse来嵌套另外一个vm时的变量共享问题。如:

->a.vm 里嵌套 b.vm;

->a.vm 里定义了变量 $param;

->b.vm 里可以直接使用$param,无任何限制。

但需要特别注意的是,如果b.vm里同时定义有变量$param,则b.vm里将使用b.vm里定义的值。

10.转义字符''的使用

如果reference被定义,两个’\’意味着输出一个’\’,如果未被定义,刚按原样输出。

如:

#set($email = "foo" )

$email

$emai

l\$email

\$email

输出:

foo

$email

\foo

$email

如果$email 未定义

$email

$email

\$email

\$email

输出:

$email

$email

\$email

\$email