Velocity template language (VTL ).

Source: Internet
Author: User
Http://velocity.apache.org/engine/releases/velocity-1.5/vtl-reference-guide.htmlAbout this Guide

This guide is the reference for the velocity template language (VTL). For more information, please also refer to the velocity user guide.

Referencesvariables

Notation:

$[!] [{] [A. Z,A. Z] [A. Z,A. Z,0 .. 9,-,_] [}]

Examples:

    • Shorthand notation: $ Mud-Slinger_9
    • Silent shorthand notation: $! Mud-Slinger_9
    • Formal Notation: $ {Mud-Slinger_9}
    • Silent formal Notation: $! {Mud-Slinger_9}
Properties

Notation:

$[{] [A. Z,A. Z] [A. Z,A. Z,0 .. 9,-,_] *.[A. Z,A. Z] [A. Z,A-Z,0 .. 9,-,_] * [}]

Examples:

    • Regular Notation: $ customer. Address
    • Formal Notation: $ {purchase. Total}
Methods

Notation:

$[{] [A. Z,A. Z] [A. Z,A. Z,0 .. 9,-,_] *.[A. Z,A. Z] [A. Z,A. Z,0 .. 9,-,_] *([Optional parameter list...])[}]

Examples:

    • Regular Notation: $ customer. getaddress ()
    • Formal Notation: $ {purchase. gettotal ()}
    • Regular notation with parameter list: $ page. settitle ("My home page ")

VTL properties can be used as a shorthand notation for VTL methods that takeGetAndSet. Either$ Object. getmethod ()Or$ Object. setmethod ()Can be abbreviated$ Object. Method. It is generally preferable to use a property when available. The main difference between properties and methods is that you can specify a parameter list to a method.

Directives # Set-establishes the value of a reference

Format:

#[{]Set[}]($Ref=[",'] Arg [",'])

Usage:

    • $ Ref-The LHS of the assignment must be a variable reference or a property reference.
    • ARG-The RHS of the assignment,ARGIs parsed if enclosed in double quotes, and not parsed if enclosed in single quotes. If the RHS evaluatesNull, It isNotAssigned to the LHS.

Examples:

    • Variable reference: # Set ($ monkey = $ Bill)
    • String Literal: # Set ($ monkey. Friend = 'monica ')
    • Property Reference: # Set ($ monkey. Blame = $ Whitehouse. Leak)
    • Method reference: # Set ($ monkey. Plan = $ spindoctor. Weave ($ web ))
    • Number literal: # Set ($ monkey. Number = 123)
    • Range OPERATOR: # Set ($ monkey. Numbers = [1 .. 3])
    • Object List: # Set ($ monkey. Say = ["not", $ my, "fault"])
    • Object map: # Set ($ monkey. Map = {"banana": "Good", "roast beef": "bad "})

The RHS can also be a simple arithmetic expression, such:

    • Addition: # Set ($ value = $ Foo + 1)
    • Subtraction: # Set ($ value = $ bar-1)
    • Multiplication: # Set ($ value = $ Foo * $ bar)
    • Division: # Set ($ value = $ Foo/$ bar)
    • Remainder: # Set ($ value = $ Foo % $ bar)
# If/# elseif/# else-output conditional on truth of statements

Format:

#[{]If[}]([Condition])[Output] [#[{]Elseif[}]([Condition])[Output] * [#[{]Else[}] [Output]#[{]End[}]

Usage:

    • Condition-If a Boolean, considered true if it has a true false; if not a Boolean, considered true if not null.
    • Output-May contain VTL.

Examples (showing different operators ):

Operator name Symbol Alternative symbol Example
Equals number = EQ # If ($ Foo = 42)
Equals string = EQ # If ($ Foo = "bar ")
Object equivalence = EQ # If ($ Foo = $ bar)
Not equals ! = Ne # If ($ foo! = $ Bar)
Greater > GT # If ($ Foo> 42)
Less < Lt # If ($ Foo <42)
Greater than or equal > = Ge # If ($ Foo> = 42)
Less than or equal <= Le # If ($ Foo <= 42)
Boolean not ! Not # If (! $ Foo)

Notes:

    1. The = operator can be used to compare numbers, strings, objects of the same class, or objects of different classes. in the last case (when objects are of different classes), The tostring () method is called on each object and the resulting strings are compared.
    2. You can also use brackets to delimit directives. This is especially useful when text immediately follows# ElseDirective.
# If ($ Foo = $ bar) It's true! # {Else} It's not! # End </LI>
# Foreach-loops through a list of objects

Format:

#[{]Foreach[}]($ RefInARG)Statement#[{]End[}]

Usage:

    • $ Ref-The first variable reference is the item.
    • ARG-May be one of the following: a reference to a list (I. e. Object array, collection, or map), an array list, or the range operator.
    • Statement-What is output each time velocity finds a valid item in the list denoted aboveARG. This output is any valid VTL and is rendered each iteration of the loop.

Examples of the # foreach (), omitting the statement block:

    • Reference: # foreach ($ item in $ items)
    • Array list: # foreach ($ item in ["not", $ my, "fault"])
    • Range OPERATOR: # foreach ($ item in [1 .. 3])

Velocity provides an easy way to get the loop counter so that you can do something like the following:

 
<Table> # foreach ($ customer in $ customerlist) <tr> <TD> $ foreach. count </TD> <TD> $ customer. name </TD> </tr> # End </table>

Additionally, the maximum allowed number of loop iterations can be controlled engine-wide (an ability introduced in velocity 1.5). By default, there is no limit:

 
# The maximum allowed number of loops. Directive. foreach. maxloops =-1
# Include-renders local file (s) that are not parsed by velocity

Format:

#[{]Include[}](Arg [arg2... argn])

    • ARG-Refers to a valid file under template_root.

Examples:

    • String: # include ("disclaimer.txt" "opinion.txt ")
    • Variable: # include ($ Foo $ bar)
# Parse-renders a local template that is parsed by velocity

Format:

#[{]Parse[}](ARG)

    • ARG-Refers to a template under template_root.

Examples:

    • String: # parse ("lecorbusier. VM ")
    • Variable: # parse ($ Foo)

Recursion permitted. SeeParse_directive.maxdepthInVelocity. PropertiesTo change from parse depth. (the default parse depth is 10 .)

# Stop-stops the template engine

Format:

#[{]Stop[}]

Usage:

This will stop execution of the current template. This is good for debugging a template.

# Break-stops the current directive

Format:

#[{]Break[}]

Usage:

This will break execution of the current content directive. this is good for exiting a # foreach loop early, but also works in other scopes. you can even pass the scope control reference for a specific outer scope to break execution of all scopes outward to the specified one.

# Evaluate-dynamically evaluates a string or reference

Format:

#[{]Evaluate[}](ARG)

    • ARG-String literal or reference to be dynamically evaluated.

Examples:

    • String: # evaluate ('string with VTL # If (true) will be displayed # end ')
    • Variable: # include ($ Foo)
# Define-assigns a block of VTL to a reference

Format:

#[{]Define[}]($ Ref)Statement#[{]End[}]

    • $ Ref-Reference that is assigned the VTL block as a value.
    • Statement-Statement that is assigned to the reference.

Example:

    • # Define ($ Hello) Hello $ who # End # Set ($ who = "world! ") $ Hello # displays Hello world!
# Macro-allows users to define a velocimacro (VM), a repeated segment of a VTL template, as required

Format:

#[{]Macro[}](Vmname $ arg1 [$ arg2 $ arg3... $ argn])[Vm vtl code...]#[{]End[}]

    • Vmname-Name used to call the VM (# Vmname)
    • $ Arg1 $ arg2 [...]-Arguments to the VM. There can be any number of arguments, but the number used at invocation must match the number specified in the definition.
    • [Vm vtl code...]-Any valid VTL code, anything you can put into a template, can be put into a VM.

Once defined, the VM is used like any other VTL directive in a template.

 
# Vmname ($ arg1 $ arg2)

Except, That when you wish to call a VM with a body, then you must prefix the name of the VM with @. The content of that body may be referenced in the macro definition via $! Bodycontent as either or few times as you like.

 
# @ Vmname ($ arg1 $ arg2) Here is the body # End

VMS can be defined in one of two places:

    1. Template Library:Can be either VMS pre-packaged with velocity or custom-made, user-defined, site-specific VMS; available from any template
    2. Inline:Found in regular templates, only usable whenVelocimacro. permissions. allowinline = trueInVelocity. Properties.
Comments

Comments are not rendered at runtime.

Single Line

Example:

# This is a comment.

Multi line

Example:

#*
This is a multiline comment.
This is the second line
*#

Unparsed content

Unparsed content is rendered at runtime, but is not parsed or interpreted.

Example:

#[[
This has invalid syntax that wowould normally need "poor man's escaping" like:

    • # Define ()
    • $ {Blah

] #

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.