velocity笔记
年青的时候使用velocity的笔记,真是还念那段日子。
其实就是吧demo里面的文章通读了后的摘抄而已,每次使用velocity前先温习一下,然后再去使用。
<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud) )
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
1 define a variable: a
#set( $a = "Velocity" )
String values are always enclosed in quotes, either single or double quotes.
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 =
References begin with $ and are used to get something. Directives begin with # and are used to do
something.
#set( $size = "Big" )
#set( $name = "Ben" )
#set($clock = "${size}Tall$name" )
The clock is $clock.
Now the output is 'The clock is BigTallBen'.
2 Comments
A single line comment begins with ## and finishes at the end of the line. If you're going to write a few
lines of commentary, there's no need to have numerous single line comments. Multi-line comments, which
begin with #* and end with *#, are available to handle this scenario.
There is a third type of comment, the VTL comment block, which may be used to store such information as
the document author and versioning information: #** third comment type*#
3 References
Everything coming to and from a reference is treated as a String object. If there is an object that
represents $foo (such as an Integer object), then Velocity will call its .toString() method to resolve
the object into a String.
Take the first example, $customer.Address. It can have two meanings. It can mean, Look in the hashtable
identified as customer and return the value associated with the key Address. But $customer.Address can
also be referring to a method (references that refer to methods will be discussed in the next section);
$customer.Address could be an abbreviated way of writing $customer.getAddress(). When your page is
requested, Velocity will determine which of these two possibilities makes sense, and then return the
appropriate value.
4 Method
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )
How to user {} to protect the $charater
Jack is a pyromaniac
Jack is a kleptomaniac
Jack is a ${vice}maniac
<input type="text" name="email" value="$email"/>
<input type="text" name="email" value="$!email"/>
<input type="text" name="email" value="$!{email}"/>
5 Getting Literal
Currency
There is no problem writing "I bought a 4 lb. sack of potatoes at the farmer's market for only $2.50!"
As mentioned, a VTL identifier always begins with an upper- or lowercase letter, so $2.50 would not be
mistaken for a reference
Escaping Valid VTL References
#set( $email = "foo" )
$email
\$email
\\$email
\\\$email
foo
$email
\foo
\$email
#set( $foo = "gibbous" )
$moon = $foo
The output is $moon = "gibbous"
6 Case Substitution
$foo
$foo.getBar()
## is the same as
$foo.Bar
$data.setUser("jon")
## is the same as
#set( $data.User = "jon" )
When the method getFoo() is referred to in a template by $bar.foo, Velocity will first try $getfoo. If
this fails, it will then try $getFoo. Similarly, when a template refers to $bar.Foo, Velocity will try
$getFoo() first and then try getfoo().
7 Directive
#if($a==1)true enough#elseno way!#end
#if($a==1)true enough#{else}no way!#end
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
For the ArrayList example the elements defined with the [..] operator are accessible using the
methods defined in the ArrayList class. So, for example, you could access the first element above using
$monkey.Say.get(0).
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map
for the Map example, the elements defined within the { } operator are accessible using
the methods defined in the Map class. So, for example, you could access the first element above using
$monkey.Map.get("bannana") to return a String 'good', or even $monkey.Map.banana to return the same
value.
#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
The RHS can also be a simple arithmetic expression:
Velocity has logical AND, OR and NOT operators as well. For further information
## logical AND
#if( $foo && $bar )
<strong> This AND that</strong>
#end
## logical OR
#if( $foo || $bar )
<strong>This OR That</strong>
#end
##logical NOT
#if( !$foo )
<strong>NOT that</strong>
#end
8 ForEach
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
The default name for the loop counter variable reference, which is specified in the velocity.properties
file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the
velocity.properties file. Here's what the loop counter properties section of the velocity.properties
file appears:
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1
9 #Include
The contents of the file are not rendered through the template engine. For security reasons, the file to
be included may only be under TEMPLATE_ROOT.
#include( "one.txt" )
The file to which the #include directive refers is enclosed in quotes. If more than one file will be
included, they should be separated by commas.
#include( "one.gif","two.txt","three.htm" )
10 Parse
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.
#parse( "me.vm" )
Any templates to which #parse refers must be included under TEMPLATE_ROOT. Unlike the #include
directive, #parse will only take a single argument.
11 #stop
The #stop script element allows the template designer to stop the execution of the template engine and
return. This is useful for debugging purposes.
12 #macro
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#set( $color = "blue" )
<table>
#tablerows( $color $greatlakes )
</table>
The OutPut:
<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>
#macro( inner $foo )
inner : $foo
#end
#macro( outer $foo )
#set($bar = "outerlala")
outer : $foo
#end
#set($bar = 'calltimelala')
#outer( "#inner($bar)" )
The output is :
Outer : inner : outerlala
What is Velocimacro Autoreloading?
There is a property, meant to be used in development, not production :
velocimacro.library.autoreload
which defaults to false. When set to true along with
<type>.resource.loader.cache = false
file.resource.loader.path = templates
file.resource.loader.cache = false
velocimacro.library.autoreload = true
13 Range Operater
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]
First example:
1 2 3 4 5
Second example:
2 1 0 -1 -2
Third example:
0 1
Fourth example:
[1..3]
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:
年青的时候使用velocity的笔记,真是还念那段日子。
其实就是吧demo里面的文章通读了后的摘抄而已,每次使用velocity前先温习一下,然后再去使用。
<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud) )
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
1 define a variable: a
#set( $a = "Velocity" )
String values are always enclosed in quotes, either single or double quotes.
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 =
References begin with $ and are used to get something. Directives begin with # and are used to do
something.
#set( $size = "Big" )
#set( $name = "Ben" )
#set($clock = "${size}Tall$name" )
The clock is $clock.
Now the output is 'The clock is BigTallBen'.
2 Comments
A single line comment begins with ## and finishes at the end of the line. If you're going to write a few
lines of commentary, there's no need to have numerous single line comments. Multi-line comments, which
begin with #* and end with *#, are available to handle this scenario.
There is a third type of comment, the VTL comment block, which may be used to store such information as
the document author and versioning information: #** third comment type*#
3 References
Everything coming to and from a reference is treated as a String object. If there is an object that
represents $foo (such as an Integer object), then Velocity will call its .toString() method to resolve
the object into a String.
Take the first example, $customer.Address. It can have two meanings. It can mean, Look in the hashtable
identified as customer and return the value associated with the key Address. But $customer.Address can
also be referring to a method (references that refer to methods will be discussed in the next section);
$customer.Address could be an abbreviated way of writing $customer.getAddress(). When your page is
requested, Velocity will determine which of these two possibilities makes sense, and then return the
appropriate value.
4 Method
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )
How to user {} to protect the $charater
Jack is a pyromaniac
Jack is a kleptomaniac
Jack is a ${vice}maniac
<input type="text" name="email" value="$email"/>
<input type="text" name="email" value="$!email"/>
<input type="text" name="email" value="$!{email}"/>
5 Getting Literal
Currency
There is no problem writing "I bought a 4 lb. sack of potatoes at the farmer's market for only $2.50!"
As mentioned, a VTL identifier always begins with an upper- or lowercase letter, so $2.50 would not be
mistaken for a reference
Escaping Valid VTL References
#set( $email = "foo" )
foo
\foo
#set( $foo = "gibbous" )
$moon = $foo
The output is $moon = "gibbous"
6 Case Substitution
$foo
$foo.getBar()
## is the same as
$foo.Bar
$data.setUser("jon")
## is the same as
#set( $data.User = "jon" )
When the method getFoo() is referred to in a template by $bar.foo, Velocity will first try $getfoo. If
this fails, it will then try $getFoo. Similarly, when a template refers to $bar.Foo, Velocity will try
$getFoo() first and then try getfoo().
7 Directive
#if($a==1)true enough#elseno way!#end
#if($a==1)true enough#{else}no way!#end
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
For the ArrayList example the elements defined with the [..] operator are accessible using the
methods defined in the ArrayList class. So, for example, you could access the first element above using
$monkey.Say.get(0).
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map
for the Map example, the elements defined within the { } operator are accessible using
the methods defined in the Map class. So, for example, you could access the first element above using
$monkey.Map.get("bannana") to return a String 'good', or even $monkey.Map.banana to return the same
value.
#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
The RHS can also be a simple arithmetic expression:
Velocity has logical AND, OR and NOT operators as well. For further information
## logical AND
#if( $foo && $bar )
<strong> This AND that</strong>
#end
## logical OR
#if( $foo || $bar )
<strong>This OR That</strong>
#end
##logical NOT
#if( !$foo )
<strong>NOT that</strong>
#end
8 ForEach
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
The default name for the loop counter variable reference, which is specified in the velocity.properties
file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the
velocity.properties file. Here's what the loop counter properties section of the velocity.properties
file appears:
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1
9 #Include
The contents of the file are not rendered through the template engine. For security reasons, the file to
be included may only be under TEMPLATE_ROOT.
#include( "one.txt" )
The file to which the #include directive refers is enclosed in quotes. If more than one file will be
included, they should be separated by commas.
#include( "one.gif","two.txt","three.htm" )
10 Parse
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.
#parse( "me.vm" )
Any templates to which #parse refers must be included under TEMPLATE_ROOT. Unlike the #include
directive, #parse will only take a single argument.
11 #stop
The #stop script element allows the template designer to stop the execution of the template engine and
return. This is useful for debugging purposes.
12 #macro
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#set( $color = "blue" )
<table>
#tablerows( $color $greatlakes )
</table>
The OutPut:
<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>
#macro( inner $foo )
inner : $foo
#end
#macro( outer $foo )
#set($bar = "outerlala")
outer : $foo
#end
#set($bar = 'calltimelala')
#outer( "#inner($bar)" )
The output is :
Outer : inner : outerlala
What is Velocimacro Autoreloading?
There is a property, meant to be used in development, not production :
velocimacro.library.autoreload
which defaults to false. When set to true along with
<type>.resource.loader.cache = false
file.resource.loader.path = templates
file.resource.loader.cache = false
velocimacro.library.autoreload = true
13 Range Operater
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]
First example:
1 2 3 4 5
Second example:
2 1 0 -1 -2
Third example:
0 1
Fourth example:
[1..3]
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: