Summary of the use of the velocity template engine

Source: Internet
Author: User

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

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 basic syntax and usage:

1."#" Is the script statement used to identify velocity.

For example: # Set, # If, # else, # foreach, # End, # include, # parse, # macro, etc.

2."$" Identifies an object (or a variable understood as PHP)

For example, $ I and $ user.

3."{}" Is used to clearly identify the velocity variable and distinguish it from a normal template string;

For example, $ {user}'s age can be displayed as riqi's age.

4."! "Force the nonexistent variables to be blank.

Example: $! MSG. If the MSG object is null, this variable is not displayed in the template. If the MSG object is missing "! ", The $ MSG string is displayed. This is the result we don't want.

5.Definition and value assignment of Variables. You do not need to specify the variable type. php, similar to the weak type language, can specify the variable type at will. After assigning values, the variable type is automatically determined, for example:

# Set ($ username = "riqi") # Set username # Set ($ age = 26) # set age

6.Array Loop:

#foreach ($user in $users)    $!{user} $!{velocityCount} <br /> #end

$ Users can be vector, hashtable, or array. Velocity provides the value of the number of loops: $ velocitycount.

7.Statement comment:

Single line comment: # single line comment code

Multi-line comment: # * multi-line comment Code *#

8.Templates support relational and logical operator operations, Such as: &, | ,! And so on

9.Macro definition: # Macro. Similar to PhP, a function is declared with the function name and parameter list. Define before calling.

10.Termination command: # Stop, similar to exit () of PHP; stop execution template engine and return.

11.Introduce public template files: # Include and # parse. Their differences are:

(1) Unlike # include, # parse can only specify a single object. # Include can have multiple

If you need to introduce multiple files, you can use commas to separate them:

#include ("one.gif", "two.txt", "three.htm" )

The file name can be in brackets, but more variables are used:

#include ( “greetings.txt”, $seasonalstock )

(2) # The content of the include file will not be parsed by the template engine;

The file content introduced by # parse velocity will parse the velocity syntax and hand it over to the template, which means it is equivalent to copying the introduced file to the file.

# Parse can be called recursively.

12.Escape Character '\'.

This is no different from other languages. For example: $ user = "riqi"; then, \ $ user indicates that the $ user string is output, and \ $ user indicates the output \ riqi.

13.Velocity has some built-in Java objectsFor example, $ request, $ response, and $ session can be called directly in the VM template.

Bytes ------------------------------------------------------------------------------------

Details:

1. velocity:

# If ($! Variable name )...... # Else ...... # End

Or:

#if("" == $!varName)……#else……#end

Summary of the use of the velocity template engine

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.