Velocity syntax tutorial

Source: Internet
Author: User
Tags arithmetic operators

 

Velocity is a Java-based template engine ). It allows anyone to simply use the template language to reference objects defined by Java code.

When velocity is applied to web development, the interface designer can develop a web site following the MVC Architecture in sync with Java program developers. That is to say, the page designer can only focus on the page display effect, java developers focus on business logic encoding. Velocity separates Java code from web pages, which facilitates the long-term maintenance of web sites, it also provides an optional solution for us outside of JSP and PHP.

Velocity is far more powerful than Web site development. For example, it can generate SQL, postscript, and XML from templates, it can also be used as an independent tool to generate source code and reports, or as an integrated component of other systems. Velocity can also provide template service for the turbine web development architecture ). Velocity + turbine provides a template service that allows a web application to be developed with a real MVC model.

The update address of velocity's Eclipse plug-in is: http://propsorter.sourceforge.net/veloeclipse

 

I. Basic syntax

1. "#" is the script statement used to identify velocity, including # Set, # If, # else, # End, # foreach, # End, # iinclude, # parse, # macro, etc;
For example:
# If ($ info. IMGs)

# Else

# End

2. "$" is used to identify an object (or understood as a variable). For example:
For example, $ I, $ MSG, $ tagutil. Options (...), etc.

3. "{}" is used to clearly identify the velocity variable;
For example, there is a $ someonename in the page. In this case, velocity uses someonename as the variable name. If our program wants to display the name character after the someone variable, then the label above should be changed to $ {someone} Name.

4 ,"! "Is used to forcibly display non-existent variables as blank.
For example, if the page contains $ MSG, the value of MSG is displayed if the MSG object has a value. If the MSG object does not exist, the $ MSG character is displayed on the page. This is not what we want. In order to display the non-stored variables or objects with null variables as blank, you only need to add a "!" before the variable name. .
Example: $! MSG

 

2. Theoretically, you can use all the velocity scripts and functions in the easyjweb template. However, we do not recommend that you use too many complex script expressions in the Interface Template, do not add any complicated logic to the Interface Template, or add variable declaration and logical operators to the Interface Template.

In easyjweb, five basic template script statements are provided to meet the requirements of all application templates. These four template statements are simple and can be directly added by the interface designer. In many easyjweb application practices, we can see that only the following four simple template script statements can be summarized in all interface templates:
1. $! OBJ returns the object result directly.
For example, the Java object MSG value is displayed 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 easyjweb open-source applications, we often see examples for pop-up message MSG.
# If ($ MSG)
<SCRIPT>
Alert ('$! MSG ');
</SCRIPT>
# End
The above script indicates that when the object MSG object exists, the content after <SCRIPT> is output.

3. # foreach ($ info in $ list) $ info. somelist # End cyclically reads the objects in the list of sets and processes them accordingly.
For example, on the homepage of easyjf open-source forum system (0.3), HTML Interface Template scripts with hot topics are displayed:
# Foreach ($ info in $ hotlist1)
<A href = "/bbsdoc. EJF? Easyjwebcommand = show & cid = $! Info. CID "target =" _ blank ">$! Info. Title </a> <br>
# End
The preceding script traverses objects in the hotlist1 set cyclically and outputs related content of objects.

4. # macro (macroname) # End Script Function (macro) calls. It is not recommended to use them in a large number in the Interface Template.
For example, in the example of adding, deleting, modifying, and querying quickly generated by easyjweb tools, you can click the title bar of the List to display the results in ascending/descending order, this is a template content that we often see in easyjweb.
Function (macro) definition, usually placed at the beginning
# Macro (orderpic $ type)
# If ($ orderfield. Equals ($ type ))

# End
# End
For example, <font color = "# ffffff"> title # orderpic ("title") </font>

After testing, macros do not support method overloading.

 

5. Include files # inclue ("template file name") or # parse ("template file name ")
It is mainly used to process pages with the same content, such as the top or tail content of each website.
For more information, see easyjf open-source blog and easyjf open-source forum!
For example: # parse ("/blog/top.html") or # include ("/blog/top.html ")
The difference between parse and include is that if the included file contains the velocity script tag, it will be further parsed, and include will be displayed as is.

 

Iii. # Set usage

Do not declare the velocity script variable on the page view, that is, use # set as little as possible. Sometimes we need to display the serial number on the page, and the program object does not include this serial number attribute, which can be customized. As shown in a circular system:
# Set ($ I = 0)
# Foreach ($ info in $ List)
No.: $ I
# Set ($ I = $ I + 1)
# End

 

Iv. Summary of velocity script syntax

1. Declaration: # Set ($ Var = xxx)
The following content can be left
Variable reference
String Literal
Property Reference
Method reference
Number literal # Set ($ I = 1)
Arraylist # Set ($ arr = ["yt1", "T2"])
Arithmetic Operators

2. Notes:
Single line ## xxx
Multiple lines # * XXX
Xxxx
Xxxxxxxxxxxx *#

References reference type
3. Variable Variables
It must start with "$" and the first character must be a letter. Character followed by a VTL identifier. (A. Z or a. Z ).
A variable can contain the following characters:
Alphabetic (A. Z, A. Z)
Numeric (0 .. 9)
Hyphen ("-")
Underscore ("_")

4. Properties
$ Identifier. identifier
$ User. Name
NAME value in hashtable user. For example: User. Get ("name ")

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

6. Formal reference notation
Use {} to separate variable names from strings

For example
# Set ($ user = "CSY "}
$ {User} Name
Return csyname

$ Username
$! Username
$ And $! Difference
When the username cannot be found, $ username returns the string "$ username", while $! 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 mode by setting stringliterals. interpolate = false.

8. Condition statements
# If ($ Foo)
<Strong> velocity! </Strong>
# End
# If ($ Foo)
# Elseif ()
# Else
# End
When $ foo is null or a Boolean object's false value, it is executed.

9. logical operators: ==&||!

10. Loop statement # foreach ($ VaR in $ arrays) // The 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

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 execution and return

15. Define the macro velocimacros, which is equivalent to the function supporting the inclusion Function
# Macro (d)
<Tr> <TD> </tr>
# End
Call
# D ()

16. macros with Parameters
# 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]

Velocity syntax tutorial

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.