I. Velocity SCRIPT Syntax Summary
1. Variables
(1) Definition of variable:
#set ($name = "Hello") Description: The variable in velocity is weakly typed.
When using the #set instruction, the literal string enclosed in double quotation marks is parsed and re-interpreted as follows: #set ($directoryRoot = "www")
#set ($templateName = "INDEX.VM")
#set ($template = "$directoryRoot/$templateName")
$template
The output will be: WWW/INDEX.VM
Note: It is not a problem to use a currency identifier such as $2.5 in velocity, because the variables in velocity always start with a letter in uppercase or lowercase.
(2) The wording of the variable specification
${name} can also be written as: $name. Promote the use of the preceding wording.
For example, you want to dynamically organize a string by using a variable $vice.
Jack is a $vicemaniac.
The original variable is $vice now becomes $vicemaniac, so veloctiy don't know what you want. Therefore, it should be written in a canonical format: Jack is a ${vice}maniac
Velocity now knows that the variable is $vice rather than $vicemaniac.
Note: You cannot add {} When referencing a property
(3) Assignment of variables:
$name = "Hello"
The left side of the assignment must be a variable or a property reference. The right side can be one of the following six types:
Variable references, literal strings, property references, method references, literal numbers, array lists.
The following example shows each of these types:
#set ($monkey = $bill) # variable reference
#set ($monkey. Friend = "Monica") # # String
#set ($monkey. Blame = $whitehouse. Leak) # # Property Reference
#set ($monkey. Plan = $spindoctor. Weave ($web)) # # Method Reference
#set ($monkey. Number = 123) # #number
#set ($monkey. Say = ["not", $my, "Fault"]) # # ArrayList
Note: ① if the right value in the above example is null, then the lvalue is not assigned, which means that the previous value is preserved. Variables that are not defined in the ②velocity template are considered to be a string. For example:
#set ($foo = "gibbous")
$moon = $foo
The output is:
$moon = gibbous
Reference is not interpreted as an instance variable of an object in the ③velocity template. For example: $foo. Name will be interpreted as the GetName () method of the Foo object, not the name instance variable of the Foo object. For example: $foo. Getbar () is equivalent to $foo. Bar;
$data. GetUser ("Jon") is equivalent to $data. User ("Jon");
Data.getrequest (). getServerName () is equivalent to
$data. Request.servername is equivalent to ${data. Request.servername}
2. Cycle
#foreach ($element in $list)
This is $element.
$velocityCount
#end
Example:
#set ($list = ["Pine", "oak", "maple"])
#foreach ($element in $list)
$velocityCount
This is $element .<br>
#end
The result of the output is:
1 This is pine.
2 This is oak.
3 This is maple.
A value in the $list of each loop is assigned to the $element variable.
$list can be a vector, Hashtable, or array. The value assigned to $element is a Java object and can be referenced by a variable. For example: if $element T is a Java product class, and the name of the product can be obtained by calling his GetName () method.
#foreach ($key in $list. KeySet ())
Key: $key, Value: $list. Get ($key) <br>
#end
Tip: Velocity is case sensitive.
Velocity also provides a way to get the number of cycles, $velocityCount the name of the variable is the default name of velocity.
Example:
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
The output of the above three examples is:
First example:
1 2 3) 4 5
Second Example:
2 1 0-1-2
Third example:
0 1
3. Conditional statements
#if (condition)
#elseif (condition)
#else
#end 4. Nesting of statements
#foreach ($element in $list) # # inner foreach inner loop #foreach ($element in $list) the is $element. $velocityCount <br>inner<br> #end # # inner foreach Inner Loop End # # outer foreach this is $element. $velocityCount <br>outer<br> #end
Statements can also be nested in other statements, such as the # if "#else" #end等.
5. Notes
(1) Single-line comment:
# # This is a single line comment.
(2) Multi-line Comment:
#*
Thus begins a multi-line comment. Online visitors won ' t see this text because the Velocity templating Engine would ignore it.
*#
(3) Document format:
#**
This is a VTL comment block and
May is used to store such information
As the document author and Versioning
Information:
@version 1.1
@author Xiao
*#
6. Relational and logical operators
Velocity also has a logical and, or, and not operator.
Such as
# # Example for and
#if ($foo && $bar)
<strong> this and that</strong>
#end
Examples where the # if () directive is true only when the $foo and $bar are true. If $foo is false, the expression is also false, and $bar will not be evaluated. If the $foo is true, the Velocity template engine will continue to check the value of $bar, and if the $bar is true, the entire expression is true. and output this and that. If the $bar is false, there will be no output because the entire expression is false.
Macros in 7.Velocity
A macro in velocity can be understood as a function.
① Definition of macros
#macro (macro name $ parameter 1 $ parameter 2 ...)
Statement body (i.e. function body)
#end
② calls to macros
#宏的名称 ($ parameter 1 $ parameter 2 ...)
Description: The parameters are separated by a space.
8. #stop
It is helpful to stop executing the template engine and return it and apply it to debug.
9. #include与 #parse
#include和 #parse's role is to introduce local files, for security reasons, the local files that are introduced can only be in the Template_root directory.
Difference:
(1) Unlike # include, #parse只能指定单个对象. And # include can have multiple
If you need to introduce multiple files, you can separate the lines with commas:
#include ("One.gif", "Two.txt", "three.htm")
The file name can be in parentheses, but more often it uses variables:
#include ("Greetings.txt", $seasonalstock)
(2) #include被引入文件的内容将不会通过模板引擎解析;
The #parse introduction of the file content velocity will parse the velocity syntax and hand it over to the template, meaning quite similar to copy the introduced file into the file. #parse是可以递归调用的, for example: If DOFOO.VM contains the following line:
Count down.<br>
#set ($count = 8)
#parse ("PARSEFOO.VM") <br>all done with dofoo.vm!
So in the PARSEFOO.VM template, you can include the following vtl:
$count
#set ($count = $count-1)
#if ($count > 0) <br>
#parse ("PARSEFOO.VM")
#else
<br>all Done with parsefoo.vm!
#end的显示结果为:
Count down.
8
7
6
5
4
3
2
1
All do with parsefoo.vm!
All do with dofoo.vm!
Note: variable sharing issues when using #parse in a VM to nest another VM. Such as:
->A.VM nested in B.VM;
The variable $param is defined in->A.VM;
->B.VM can be used directly in the $param without any restrictions.