Volicity Grammar Study and summary

Source: Internet
Author: User

Velocity is a Java-based template engine. It allows anyone to refer to objects defined by Java code simply by using template language (language). When velocity is applied to web development, interface designers can synchronize with Java program developers to develop a Web site that adheres to the MVC architecture, which means that page designers can focus only on the display of the page, while the Java program developer focuses on the business logic encoding. Velocity separates the Java code from the Web page, which facilitates long-term maintenance of the Web site, and provides an optional alternative to JSP and PHP.
Velocity's ability is much more than web site development, for example, it can generate SQL and PostScript, XML from Templates (template), which can also be used as a standalone tool to generate source code and reports, or as an integration component of other systems. Velocity can also provide template services for the turbine Web development architecture. Velocity+turbine provides a template service in a way that allows a Web application to be developed with a real MVC model.

First, the basic grammar

1, "#" is used to identify the velocity of the script statement, including #set, #if, #else, #end, #foreach, #end, #include, #parse, #macro等;
Such as:
#if ($info. IMGs)

#else

#end

2. "$" is used to identify an object (or to be interpreted as a variable), such as: $i, $msg, $TagUtil. Options (...) such as

3, "{}" is used to clearly identify velocity variables;
For example, in a page where there is a $someonename, velocity will use Someonename as the variable name, and if our program wants to display the name character immediately after the someone variable, the label above should be changed to ${someone} Name

4, "!" Used to force the non-existent variable to appear blank.
If the page contains $msg, if the MSG object has a value, the value of MSG is displayed, and if no MSG object is present, the $msg character is displayed in the page. This is what we do not want, in order to display the non-existent variable or variable value null object as blank, you only need to add a "!" in front of the variable name. Number can be.
such as: $!msg

Ii. 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, and in the last resort, do not include any complex logic in the interface template, let's not include the variable declaration in the interface template, logical operators, and so on.
1. $!obj directly returns the result of the object.
For example: Displays the value of the Java object Msg in the HTML tag. <p>$!msg</p>
Displays the value of the Msg object after the Htmlutil object has been processed in the HTML tag <p>$! Htmlutil.dosomething ($!msg) </p>

2. #if ($!obj) #else #end Judgment statement
For example: in easyjweb various open source applications, we often see examples of MSG for pop-up cue messages.
#if ($msg)
<script>
Alert (' $!msg ');
</script>
#end
The above script indicates that when the object Msg object exists, output <script> and so on after the content.

3. #foreach ($info in $list) $info. Somelist #end Loop to read the objects in the collection list and handle them accordingly.
such as: EASYJF Open Source Forum system on (0.3) altar home Display popular theme of the HTML interface template script:
#foreach ($info in $hotList 1)
<a href= "/bbsdoc.ejf?easyjwebcommand=show&&cid=$!info.cid"
target= "_blank" >$!info.title</a><br>
#end
The above script means looping through the objects in the HotList1 collection and outputting the object's related content.
4, #macro (macroName) #end script function (macro) calls, is not recommended in the interface template is used extensively. A macro in velocity can be understood as a function.   ① macro definition #macro (macro name $ parameter 1 $ parameter 2 ...) Statement body (that is, function body) #end
② macro Call # Macro name ($ parameter 1 $ parameter 2 ...)
Description: The parameters are separated by a space.
For example, in a quick-to-use easyjweb tools sample, you can click on the title bar of the list to sort the display, which is the template content that we often see in the Easyjweb app for a sort status display.
function (macro) definition, usually on the front
#macro (Orderpic $type)
#if (erField. Equals ($type))

#end
#end
Specific calls such as: <font color= "#FFFFFF" > Title #orderpic ("title") </font>

5. Include file #inclue ("template file name") or #parse ("template file name")
Primarily used to process pages with the same content, such as the top or end content of each site.
such as: #parse ("/blog/top.html") or # include ("/blog/top.html")
Difference:
1 if there is a velocity script tag in the included file, it will be further parsed, and the include will be
Show.
2 #parse只能指定单个对象. And # include can have multiple
Demo Code:
#include ("One.gif", "Two.txt", "three.htm")
#parse ("PARSEFOO.VM")
Third, about the use of #set

In the last resort, do not declare velocity script variables yourself in Page view, that is, use #set sparingly. Sometimes we need to display the serial number in the page, and the program object does not contain the same ordinal attribute, it can be defined by itself. As in a loop system, as follows:
#set ($i =0)
#foreach ($info in $list)
Sequence Number: $i
#set ($i = $i + 1)
#end
 
Iv. Velocity Script Syntax Summary

Statement: #set ($var =xxx)
Variable references, literal strings, property references, method references, literal numbers, array lists.
#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
Arithmetic operators
#set ($foo = $bar + 3) #set ($foo = $bar-4) #set ($foo = $bar * 6) #set ($foo = $bar/2)

2. Comments:
Single line # XXX
Multiple lines #* XXX
Xxxx
xxxxxxxxxxxx*#

Types of References references
3. Variable Variables
Starting with "$", the first character must be a letter. Character followed by a VTL Identifier. (a). Z or A. Z).
A variable can contain characters that have the following contents:
Alphabetic (a). Z, A.. Z)
Numeric (0.. 9)
Hyphen ("-")
Underscore ("_")

4. Properties
$Identifier. Identifier
$user. Name
Hashtable the name value in user. Similar to: User.get ("name")

5, Methods
Object User.getname () = $user. GetName ()

6, formal Reference Notation
Use {} to separate variable names from strings

Such as
#set ($user = "Csy"}
${user}name
Back to Csyname

$username
$!username
The difference between $ and $!
When username is not found, $username returns the string "$username" and $!username returns an 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 by setting Stringliterals.interpolate=false

8. Conditional statements
#if ($foo)
<strong>Velocity!</strong>
#end
#if ($foo)
#elseif ()
#else
#end
When $foo is null or is a Boolean object, the value of false is executed.

9. Logical operators: = = && | | !

10. Loop Statement #foreach ($var in $arrays)//set contains the following three vectors, 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
The nested #foreach of the statement ($element in $list) # # inner foreach inner loop #foreach ($element in $list) the IS $elem Ent. $velocityCount <br>inner<br> #end # # inner foreach Inner Loop End # # outer foreach this is $ele     ment. $velocityCount <br>outer<br> #end



11. The velocitycount variable is defined in the configuration file
# 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 Stop execution and return
It is helpful to stop executing the template engine and return it and apply it to debug.

15. Define macro Velocimacros, equivalent to function support include function
#macro (d)
<tr><td></td></tr>
#end
Call
#d ()

16. Macros with parameters
#macro (tablerows $color $somelist)
#foreach ($something in $somelist)
&LT;TR&GT;&LT;TD bgcolor= $color > $something </td></tr>
#end
#end

17. Range Operator
#foreach ($foo in [1..5])

18. Escape character
If reference is defined, two ' \ ' means output a ' \ ', if not defined, just as it is output. #set ($email = "foo") $email \ $email \ \ $email \\\ $email
Output: Foo $email ....

Volicity Grammar Study and summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.