<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud) )
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
|
#set( $a = "Velocity" )
|
<html>
<body>
#set( $foo = "Velocity" )
Hello $foo World!
</body>
<html>
|
## This is a single line comment.
|
This is text that is outside the multi-line comment.
Online visitors can see it.
#*
Thus begins a multi-line comment. Online visitors won't
see this text because the Velocity Templating Engine will
ignore it.
*#
Here is text outside the multi-line comment; it is visible.
|
This text is visible. ## This text is not.
This text is visible.
This text is visible. #* This text, as part of a multi-line comment,
is not visible. This text is not visible; it is also part of the
multi-line comment. This text still not visible. *# This text is outside
the comment, so it is visible.
## This text is not visible.
|
#**
This is a VTL comment block and
may be used to store such information
as the document author and versioning
information:
@author
@version 5
*#
|
$foo
$mudSlinger
$mud-slinger
$mud_slinger
$mudSlinger1
|
#set( $foo = "bar" )
|
$customer.Address
$purchase.Total
|
$customer.getAddress()
$purchase.getTotal()
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )
|
sun.getPlanets()
$annelid.getDirt()
$album.getPhoto()
|
$sun.getPlanet( ["Earth", "Mars", "Neptune"] )
##
不能将参数列表传递给
$sun.Planets
$sisyphus.pushRock()
## Velocity
假定我意思是
$sisyphus.getRock()
$book.setTitle( "Homage to Catalonia" )
##
不能传递一个参数列表
|
${mudSlinger}
${customer.Address}
${purchase.getTotal()}
|
Jack is a $vicemaniac.
|
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}"/>
|
foo
$email
/foo
/$email
|
## The following line defines $email in this template:
#set( $email = "foo" )
$email
/$email
//$email
///$email
|
foo
$email
/foo
/$email
|
$email
/$email
//$email
///$email
|
$email
/$email
//$email
///$email
|
#set( $foo = "gibbous" )
$moon = $foo
|
$foo
$foo.getBar()
## is the same as
$foo.Bar
$data.getUser("jon")
## is the same as
$data.User("jon")
$data.getRequest().getServerName()
## is the same as
$data.Request.ServerName
## is the same as
${data.Request.ServerName}
|
#set( $primate = "monkey" )
#set( $customer.Behavior = $primate )
|
#set( $monkey = $bill ) ## variable reference
#set( $monkey.Friend = "monica" ) ## string literal
#set( $monkey.Blame = $whitehouse.Leak ) ## property reference
#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference
#set( $monkey.Number = 123 ) ##number literal
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
|
#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
|
#set( $result = $query.criteria("name") )
The result of the first query is $result
#set( $result = $query.criteria("address") )
The result of the second query is $result
|
The result of the first query is bill
The result of the second query is bill
|
#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
#set( $result = $query.criteria($criterion) )
#if( $result )
Query was successful
#end
#end
|
#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
#set( $result = false )
#set( $result = $query.criteria($criterion) )
#if( $result )
Query was successful
#end
#end
|
#set( $directoryRoot = "www" )
#set( $templateName = "index.vm" )
#set( $template = "$directoryRoot/$templateName" )
$template
|
www/index.vm
|
#set( $foo = "bar" )
$foo
#set( $blargh = '$foo' )
$blargh
|
Bar
$foo
|
#if( $foo )
<strong>Velocity!</strong>
#end
|
#if( $foo < 10 )
<strong>Go North</strong>
#elseif( $foo == 10 )
<strong>Go East</strong>
#elseif( $bar == 6 )
<strong>Go South</strong>
#else
<strong>Go West</strong>
#end
|
#set ($foo = "deoxyribonucleic acid")
#set ($bar = "ribonucleic acid")
#if ($foo == $bar)
In this case it's clear they aren't equivalent. So...
#else
They are not equivalent and this will be the output.
#end
|
## 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
|
<ul>
#foreach( $product in $allProducts )
<li>$product</li>
#end
</ul>
|
<ul>
#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end
</ul>
|
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
|
# 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
|
#include( "one.txt" )
|
#include( "one.gif","two.txt","three.htm" )
|
#include( "greetings.txt", $seasonalstock )
|
#parse( "me.vm" )
|
Count down.
#set( $count = 8 )
#parse( "parsefoo.vm" )
All done with dofoo.vm!
|
$count
#set( $count = $count - 1 )
#if( $count > 0 )
#parse( "parsefoo.vm" )
#else
All done with parsefoo.vm!
#end
|
#stop
|
#macro( d )
<tr><td></td></tr>
#end
|
#d()
|
#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>
|
<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>
|
#set( $parts = ["volva","stipe","annulus","gills","pileus"] )
#set( $cellbgcol = "#CC00FF" )
<table>
#tablerows( $cellbgcol $parts )
</table>
|
<table>
<tr><td bgcolor="#CC00FF">volva</td></tr>
<tr><td bgcolor="#CC00FF">stipe</td></tr>
<tr><td bgcolor="#CC00FF">annulus</td></tr>
<tr><td bgcolor="#CC00FF">gills</td></tr>
<tr><td bgcolor="#CC00FF">pileus</td></tr>
</table>
|
#macro( callme $a )
$a $a $a
#end
#callme( $foo.bar() )
|
#set( $myval = $foo.bar() )
#callme( $myval )
|
## #include( "a.txt" ) renders as <contents of a.txt>
#include( "a.txt" )
## /#include( "a.txt" ) renders as /#include( "a.txt" )
/#include( "a.txt" )
## //#include ( "a.txt" ) renders as /<contents of a.txt>
//#include ( "a.txt" )
|
#if( $jazz )
Vyacheslav Ganelin
#end
|
Vyacheslav Ganelin
|
/#if( $jazz )
Vyacheslav Ganelin
/#end
|
#if($ jazz )
Vyacheslav Ganelin
#end
|
//#if( $jazz )
Vyacheslav Ganelin
//#end
|
/ Vyacheslav Ganelin
/
|
///#if( $jazz )
Vyacheslave Ganelin
//#end
|
#set( $imperial = ["Munetaka","Koreyasu","Hisakira","Morikune"] )
#foreach( $shogun in $imperial )
$shogun
#end
|
Send me #set($foo = ["$10 and ","a cake"])#foreach($a in $foo)$a #end please.
|
Send me
#set( $foo = ["$10 and ","a cake"] )
#foreach( $a in $foo )
$a
#end
please.
|
Send me
#set($foo = ["$10 and ","a cake"])
#foreach ($a in $foo )$a
#end please.
|
#set( $foo = $bar + 3 )
#set( $foo = $bar - 4 )
#set( $foo = $bar * 6 )
#set( $foo = $bar / 2 )
|
#set( $foo = $bar % 5 )
|
[n..m]
|
第一个例子
#foreach( $foo in [1..5] )
$foo
#end
第二个例子
#foreach( $bar in [2..-2] )
$bar
#end
第三个例子
#set( $arr = [0..1] )
#foreach( $i in $arr )
$i
#end
第四个例子
[1..3]
|
1 2 3 4 5
2 1 0 -1 -2
0 1
[1..3]
|
#set( $foo = "bar" )
$/!foo
$/!{foo}
$//!foo
$///!foo
|
$!foo
$!{foo}
$/!foo
$//!foo
|
/$foo
/$!foo
/$!{foo}
//$!{foo}
|
/$foo
/$!foo
/$!{foo}
/bar
|
#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
|
#macro( foo $color )
<tr bgcolor=$color><td>Hi</td></tr>
<tr bgcolor=$color><td>There</td></tr>
#end
#foo( $bar.rowColor() )
|
#set($color = $bar.rowColor())
#foo( $color )
|
file.resource.loader.path = templates
file.resource.loader.cache = false
velocimacro.library.autoreload = true
|
#set( $size = "Big" )
#set( $name = "Ben" )
The clock is $size$name.
|
#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 BigTallBen'
。
|