1. Basic grammar
1. "#" is used to identify Velocity script statements, including #set, #if, #else, #end, #foreach, #end, #iinclude, #parse, #macro, etc.;
like:
#if($info.imgs)
<img src="http://VeVB.COm/ydmx_lei/blog/$info.imgs" border=0>
#else
<img src="http://VeVB.COm/ydmx_lei/blog/noPhoto.jpg">
#end
2. "$" is used to identify an object (or understood as a variable); such as
Such as: $i, $msg, $TagUtil.options(...), etc.
3. "{}" is used to clearly identify the Velocity variable;
For example, in the page, there is $someonename. At this time, Velocity will use someonename as the variable name. If our program wants to display the name character immediately after the variable someone, the above label should be changed to ${someone. }name.
4. "!" is used to force non-existing variables to be displayed as blank.
For example, when the page contains $msg, if the msg object has a value, the value of msg will be displayed. If there is no msg object, the $msg character will be displayed on the page. This is what we don't want. In order to display non-existent variables or objects with null variable values as blank, you only need to add a "!" sign before the variable name.
Such as: $!msg
2. Best practices in EasyJWeb
Theoretically, you can use all Velocity scripts and functions in the EasyjWeb template, but we do not recommend that you use too many complex script expressions in the interface template. As a last resort, do not add any complex logic to the interface template. , let alone add variable declarations, logical operators, etc. to the interface template.
In EasyJWeb, we provide five basic template script statements, which can basically meet the requirements of all application templates. These four template statements are very simple and can be added directly by the interface designer. In many current EasyJWeb application practices, we see that only the following four simple template script statements can be implemented among all interface templates:
1. $!obj directly returns the object result.
For example: display the value of the java object msg in the html tag. <p>$!msg</p>
Display the value of the msg object processed by the HtmlUtil object in the html tag <p>$!HtmlUtil.doSomething($!msg)</p>
2. #if($!obj) #else #end judgment statement
For example: In various open source applications of EasyJWeb, we often see examples of pop-up prompt messages msg.
#if($msg)
<script>
alert('$!msg');
</script>
#end
The above script means that when the object msg object exists, the following content such as <script> will be output.
3. #foreach( $info in $list) $info.someList #end Loops to read the objects in the collection list and process them accordingly.
For example: the html interface template script for displaying popular topics on the forum homepage of the EasyJF open source forum system (0.3):
#foreach( $info in $hotList1)
<a href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank">$!info.title</a><br>
#end
The above script indicates that it loops through the objects in the hotList1 collection and outputs the relevant content of the object.
4. #macro(macroName)#end is a script function (macro) call and is not recommended for extensive use in interface templates.
For example: In the example of adding, deleting, modifying and querying quickly generated using EasyJWeb Tools, you can click on the title bar of the list to display the ascending and descending order. This is a template content that we often see in EasyJWeb applications to display the sorting status.
Function (macro) definition, usually placed first
#macro(orderPic $type)
#if ($orderField.equals($type))
<img src="http://VeVB.COm/ico/${orderType}.gif">
#end
#end
The specific call is: <font color="#FFFFFF">title#orderPic("title")</font>
5. Include the file #inclue("template file name") or #parse("template file name")
Mainly used to handle pages with the same content, such as the top or bottom content of each website.
For usage methods, you can refer to the applications in EasyJF Open Source Blog and EasyJF Open Source Forum!
Such as: #parse("/blog/top.html") or #include("/blog/top.html")
The difference between parse and include is that if there is a Velocity script tag in the included file, it will be further parsed, while include will be displayed as is.
3. About the use of #set
As a last resort, do not declare Velocity script variables yourself in the page view, that is, use #set as little as possible. Sometimes we need to display a serial number on the page, but the program object does not contain this serial number attribute. You can define it yourself. For example, in a cyclic system, it is as follows:
#set ($i=0)
#foreach($info in $list)
Serial number:$i
#set($i=$i+1)
#end
4. Velocity script syntax summary
1. Statement: #set ($var=XXX)
The left side can contain the following content
Copy the code code as follows:
Variable reference
String literal
Property reference
Method reference
Number literal #set ($i=1)
ArrayList #set ($arr=["yt1","t2"])
arithmetic operators
2. Note:
Single line ## XXX
Multiple lines #* xxx
xxxx
xxxxxxxxxxxx*#
References reference type
3. Variables
Starts with "$", the first character must be a letter. character followed by a VTL Identifier. (a .. z or A .. Z).
Variables can contain the following characters:
alphabetic (a .. z, A .. Z)
numeric (0 .. 9)
hyphen ("-")
underscore ("_")
4.Properties
$Identifier.Identifier
$user.name
The name value in hashtable user. Similar: user.get("name")
5. Methods
object user.getName() = $user.getName()
6.Formal Reference Notation
Use {} to separate variable names from strings
like
#set ($user="csy"}
${user}name
return csyname
$username
$!username
The difference between $ and $!
When username is not found, $username returns the string "$username", and $!username returns the empty string ""
7. Double quotation marks and quotation marks
#set ($var="helo")
test"$var" returns testhello
test'$var' returns test'$var'
You can change the default processing method by setting stringliterals.interpolate=false
8. Conditional statements
Copy the code code as follows:
#if( $foo )
<strong>Velocity!</strong>
#end
#if($foo)
#elseif()
#else
#end
Executed when $foo is null or the false value of a Boolean object.
9. Logical operators: == && || !
10. Loop statement #foreach($var in $arrays) //The collection contains the following three types of Vector, a Hashtable or an Array
#end
#foreach( $product in $allProducts )
<li>$product</li>
#end
#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
11. The velocityCount variable is defined in the configuration file
Copy the code code as follows:
# 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
12. Include files
#include( "one.gif","two.txt","three.htm" )
13. Parse import script
#parse("me.vm" )
14. #stop stops execution and returns
15. Define macro Velocimacros, which is equivalent to function support including functions
#macro(d)
<tr><td></td></tr>
#end
call
#d()
16. Macros with parameters
Copy the code code as follows:
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
17. Range Operator
#foreach( $foo in [1..5] )