当前位置: 代码迷 >> Web前端 >> velocity 札记
  详细解决方案

velocity 札记

热度:835   发布时间:2012-08-25 10:06:20.0
velocity 笔记

Single quotes will ensure that the quoted value will be assigned to the reference as is. Double quotes allow you to use velocity references and directives to interpolate, such as "Hello $name", where the $name will be replaced by the current value before that string literal is assigned to the left hand side of the =


#set( $a = "Velocity" )

单引号不解析 双引号解析

## This is a single line comment.

The Property $customer.Address has the exact same effect as using the Method $customer.getAddress()

<input type="text" name="email" value="$!email"/> !表示可为空 否则输出? $email

Now when the form is initially loaded and $email still has no value, an empty string will be output instead of "$email".

References to instance variables in a template are not resolved. Only references to the attribute equivalents of JavaBean getter/setter methods are resolved (i.e. $foo.Name does resolve to the class Foo's getName() instance method, but not to a public Name instance variable of Foo).

#[[don't parse me!]]#

<table>
#foreach( $customer in $customerList )
??? <tr><td>$foreach.count </td><td>$customer.Name</td></tr>
#end
</table>

#foreach( $customer in $customerList )
??? $customer.Name#if( $foreach.hasNext ),#end
#end

?$foreach.index? ? 0开头
$foreach.first and $foreach.last

$foreach.parent or $foreach.topmost

#break

#include ----? The contents of the file are not rendered through the template engine.

The #parse script element allows the template designer to import a local file that contains VTL. Velocity will parse the VTL and render the template specified.

(e.g. #break($macro)). This will stop rendering of all scopes up to the specified one

#break($foreach.parent

The #stop directive stops any further rendering and execution of the template.
短路方式,render马上结束

The example below will display abc.

#set($source1 = "abc")
#set($select = "1")
#set($dynamicsource = "$source$select")
## $dynamicsource is now the string '$source1'
#evaluate($dynamicsource)


#define( $block )Hello $who#end
#set( $who = 'World!' )
$block
display Hello World!


Note that the range operator only produces the array when used in conjunction with #set and #foreach directives