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'.