The Velocity User Guide is intended to help page designers and content providers understand Velocity and its simple and powerful scripting language (Velocity Template Language (VTL)). There are many examples in this guide to showcase the dynamic content embedded in the website with Velocity, but all VTL Examples are used with all the pages and templates.
Thank you for choosing velocity!
2. What is Velocity?
Velocity is a Java-based template engine. It allows web page designers to reference Java code predefined methods. Web designers can work according to MVC mode and Java programmers, which means that web designers can focus on design a good site, and programmers can focus alone to write underlying code. Velocity separated the Java code from the web page, so that the site still has good maintenance after running for a long time, and provides a viable option other than JSP and PHP.
Velocity can be used from templates to generate web pages, SQL, PostScript, and other outputs. He can also be used in a separate program to generate source code and reports, or as an integrated component of other systems. After this project is complete, Velocity will provide template services to the Turbine Web Application Framework. The template service provided by the Velocity Turbine scenario will allow web applications to develop in real MVC mode.
3. What can Velocity?
3.1. Mud Store Example
Suppose you are a page designer specially selling mud (MUD) online store. We call him "The Online Mud Store". Good business. Customers order a variety of types and numbers of mud. They use their username and password to log in to the store, you can browse their order and purchase other things. Now, terracotta ceramics is promoting, this is a very common mud. A small number of customers are very regularly purchased a bright red soil Bright Red Mud, which is also promotional products, but is not commonly used, so they are moved to the edge of the page. All customer information is tracked in the database, so there is a day question: Why don't you use Velocity to locate target customers, these customers are particularly interested in some types of products?
Velocity is very easy to customize (personalized) for web pages for visitors. As a site designer in the online mud shop, I want to see the page they want to see after the customer is logged in.
You encounter your company's software engineers, everyone thinks $ Customer will keep the customer information currently logged in, and $ MudsonSpecial will currently have all the mud. $ FLOGGER object contains methods that help promotion. For the current task, let us only pay attention to these three issues. Remember, you don't need to worry about how software engineers get customer information from the database, but you have to know they can. This allows you to focus on your work and software engineers are busy with their own work.
You can embed the following VTL statement in your page:
Hello $ Customer.Name!
#foreach ($ MUD IN $ MUDSONSPECIAL)
#if ($ Customer.Haspurchased ($ MUD))
$ FLOGGER.GETPROMO ($ MUD)
td>
TR>
#end
#end
TABLE>
The details of the Foreach statement will be further detailed, but it is important that this short script can actually run on your site. When a customer who tends to brightens the red soil login, bright red soil is promoting, this is what this customer is seen, and the promotion shows very significant. If another customer who has long purchased red clay is logged in, the prompt information of the red clay promotion should be in the middle of the previous position. Velocity is very flexible, limited to your creativity. Write other Velocity elements in the VTL reference documentation, they give you very powerful and flexibility to create a good site. To be more understanding of these elements, you can start to release the powerful power of Velocity.
4. Velocity Template Language (VTL): Introduction
Velocity Template Language (VTL) is designed to provide the most easily, simple, and concise methods for web pages. Even page designers who have a little or no programming can provide dynamic content for the page.
VTL uses references to embed the dynamic content into the web page, each variable is a reference for a certain type. Variables are actually a reference that can invoke content defined in the Java code, or it can derive itself from the VTL statement within the page. Here is an example in which the VTL statement can be embedded in the HTML document.
#set ($ a = "velocity")
This VTL statement is like all VTL statements, starting with # characters and follows a command set. When an online access this request page, the Velocity model engine searches all # characters in the page, then determines which one tagged the VTL statement, which tag does not require VTL to do what action.
#The follow .. The Set instruction uses an expression in parentheses - one equation assigns a value to a variable. The variable is in the left side of the equivalent value in the right side of the equivalent.
In the example above, the variable is a value of Velocity. This variable is like other references, starting with a $ character. Value is usually in quotes, for Velocity, generally no type of conflict, because only strings (text-based information) can be passed to variables.
The following main rules may help understand how Velocity works: Quote is used to get what to do with $ started, but instructions start what to do.
In the above example, # set is used to assign a value to a variable. The variable $ A can be used to output "velocity" in the template.
5. Hello Velocity World!
Once a value is assigned to a variable, you can reference it in HTML. In the example below, first assign a value to the Variable $ FOO and then reference it.
#set ($ foo = "velocity")
Hello $ foo world!
body>
The result of this page is to output "Hello Velocity World!".
In order to make the statement containing the VTL instructions readability, we encourage each VTL statement to start in a new line, although not necessarily doing this. SET will then explain in depth.
6. Comment
You can use annotations to join the descriptive text, they are not output in the template engine. Note You can help your memory or others explain what your VTL statement is doing.
## this is a single line commernd.
Single row comments start in ## and end in the Bank. If you need to join multiple lines, you don't need to add a lot of single-line comments. Multi-line comments, starting with # * and at * # can handle this situation.
This is text thing is outside the multi-line commer.
Online Visitors Can See it.
# *
Thus Begins a Multi-Line Comment. Online Visitors Wo't
See this text Because The Velocity Templating Engine Will
Ignore IT.
* #
Here is text outside the multi-line commer; it is visible.
Some examples have explained how untrue annotations and how multi-line comments work.
This text is visible. ## TEXT IS NOT.
This text is visible.
This text is visible. # * This text, as part of a multi-line comment,
Is Not Visible. This Text Is Not Visible; It is Also Part of The
Multi-line Comment. this text still not visible. * # this text is outside
The comment, so it is visible.
## THIS TEXT IS Not Visible.
There is also a third annotation, VTL comment block, can be used to store such as document author, version information, etc.
# **
This is a vtl comment block and
May Be buy to store such information
as the document author and versioning
Information:
@Author
@Version 5
* #
7. Quote
There are three types of references in VTL: variables, attributes, and methods. As designers using VTL, you and your engineers must be consistent in drinking specific names in your template to use them correctly.
All parameters for reference are processed as string objects. EVERYTHING COMING TO AND FROMEN A STRING Object. If an object indicates $ foo (such as an integer object), Velocity will call its toString () method to convert this object to a string.
7.1. Variable Variables
The simple mark of the variable is a leader "$" character, composed with an VTL identifier (Identifier.). A VTL identifier must begin with a letter (a .. z or a .. z). The remaining characters will consist of the following types of characters:
Letters (a .. z, a .. z)
Numbers (0 .. 9)
Connect characters ("-")
Underline ("_")
Here are some effective variables:
$ foo
$ mudslinger
$ MUD-SLINGER
$ MUD_SLINGER
$ mudslinger1
When VTL references a variable, such as $ foo, the variable can be obtained from the template's set instruction, or from
Get in the Java code. For example, if the Java Variable $ FOO has a value bar when the template is requested, BAR will replace all the instances of all $ foo in the page. Or, if the following statement is included:
#set ($ foo = "bar")
The output of all the instances of all $ foo after the instruction will be the same value.
7.2. Attribute
The second element referenced by the VTL is attribute, and the attribute has a unique format. Attribute's simple label Impact leader is followed by an VTL identifier, followed by a point number ("."), And then a VTL identifier. This is some effective examples: $ Customer.Address
$ purchase.total
Please see the first example, $ Customer.Address.. He has two interests. It can mean that the query is returned by the Customer marked hash table and press the keyword Address. But $ Customer.Address may also reference a method (below, $ Customer.Address may be abbreviation. When a page is requested, Velocity will determine which one may be, then Returns the corresponding value.
7.3. Method
Methods are defined in the Java code and make some useful things, such as running a calculator or make a decision. The method is actually referenced, followed by a VTL identifier after "$", followed by a VTL Body. The VTL method is followed by a VTL identifier followed by a left bracket, followed by the optional parameter list, and finally the right bracket. Here are some effective methods examples:
Two examples - $ Customer.Getdress () and $ purchase.gettotal () - look like the looks used in the property section, $ Customer.Address and $ Purchase.total .. If you think these examples are related to some aspects, then you are right.
The VTL property can be used as a simple tag for the VTL method. Attribute $ Customer.Address has the same effect as Customer.GetDress (). The main differences between attributes and methods are the list of parameters can be added in the method.
A short marking can be used in the following method:
Sun.getPlanets ()
$ Annelid.getdirt ()
$ album.getphoto ()
We may wish to put back the name of the planet belonging to the solar system, feeding our cockroaches, or returning a photo from the album. The following only long tags is a way to work:
$ sun.getplanet (["Earth", "MARS", "Neptune"])
## Can't pass the parameters to $ sun.planets
$ sisyphus.pushrock ()
## Velocity assumes me mean $ sisyphus.getrock ()
$ book.settitle ("Homage to Catalonia)
## Can't pass a parameter list
7.4. Form Reference Formal Reference Notation
The briefed symbols are as described above, but there is another reference form symbol, and examples are as follows:
$ {mudslinger}
$ {Customer.Address}
$ {purchase.gettotal ()}
In most cases, we will use a reference to the brief symbol, but in some cases, we also need to support the form of forms to correctly handle it.
Assume that you are a sentence on the sheet of paper, will use $ VICE as the root of the syllable. Our goal is to allow people to choose stems and then produce one of the following two results: "Jack is a pyromaniac."
Or "Jack Is A Kleptomaniac.".
In this case, it is not very sufficient to use a brief symbol. Taking into account the following example:
Jack is a $ ViceManiac.
There is an uncertainty here, Velocity assumes $ ViceManiac, (not $ vice) is an identifier you want to use. Can't find a value of $ ViceManiac, he will return $ ViceManiac. Use the form symbol to solve this problem:
Jack is a $ {vice }maniac
Now Velocity knows $ vice (not $ ViceManiac) is a reference. Form symbols are commonly used in drinking 咋 templates and text directly adjacent to text.
7.5. Quiet Reference Quiet Reference Notation
When Velocity encounters a bit-defined reference, its usual behavior is an image that outputs this reference. For example, suppose the following references appear in the template:
When the form is initially loaded, the variable reference $ Email does not value, you would rather be a blank domain instead of having a value "$ Email". Using a quiet reference can bypass the regular behavior of Velocity, do not use $ email in VTL to use $! Email symbol. Therefore, the above example will look like the following:
Now, when the form is initially loaded, $ Email still does not have a value, but the empty string is output instead of "$ email".
The form and quiet reference can be used together, as shown below: