VTL syntax reference

Source: Internet
Author: User

For more information, see: http://velocity.apache.org/engine/devel/user-guide.html

1. variable definition

Valid Character Set of variable name:

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

Examples:

  • General mode: $ mud-Slinger_9
  • Static (output original literal): $! Mud-Slinger_9
  • Regular format: $ {mud-Slinger_9}

2. Access attributes

Format rules:

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

Examples:

  • General Format: $ customer. Address: Call the getaddress () command of the customer object.
  • Regular format: $ {purchase. Total}

3. Command call

Format rules:

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

Examples:

  • General write code: $ customer. getaddress ()
  • Regular Syntax: $ {purchase. gettotal ()}
  • Incoming call parameters: $ page. settitle ("My home page ")

The attribute call of VTL can be interpreted as the short method of command call. Generally, the get/SET command of the object is called.

3. Action commands

1. # Set-create a reference to the value of a variable pair

Format rules:

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

Examples:

  • Variable reference: # Set ($ monkey = $ Bill)
  • Reference the original string: # Set ($ monkey. Friend = 'monica ')
  • Attribute reference: # Set ($ monkey. Blame = $ Whitehouse. Leak)
  • Command reference: # Set ($ monkey. Plan = $ spindoctor. Weave ($ web ))

Directly reference a number: # Set ($ monkey. Number = 123)

  • List assignment reference: # Set ($ monkey. Numbers = [1 .. 3])
  • Object array: # Set ($ monkey. Say = ["not", $ my, "fault"])

The right value can also be used as an expression, as follows: add, subtract, Cheng, divide and modulo:

  • 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)

2. # If/# elseif/# else-condition judgment

Format rules:

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

Usage:

  • Condition-If it is Boolean, it is determined by true or false. Otherwise, it is considered true if it is not null.
  • Output-It can contain VTL output content.

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)

Note:

1. "=" operations can be used to compare numbers, strings, or different objects of the same class or different types. when it is an object of different classes, they will call their tostring () command results for comparison to see if they are equal.

2. It can also be used as follows, but note that else should be included in.

# If ($ Foo = $ bar) It's true! # {Else} It's not! # End </LI>

3. # foreach --- iterate objects through the list using Loops

Format:

#[{]Foreach[}]( $ Ref In ARG ) Statement #[{]End[}]

Usage:

  • $ Ref-The referenced object to be iterated.
  • ARG-It may be: A List reference (I. e. Object array, collection, or map), an array list, or other lists.
  • Statement-When velocity finds the next valid object in the list, the output can be a valid VTL.

Example # foreach () usage ,:

  • Reference: # foreach ($ item in $ items)
  • Array list: # foreach ($ item in ["not", $ my, "fault"])
  • According to the set boundary: # foreach ($ item in [1 .. 3])

You can obtain the current value of the number of cycles as follows:

<Table>

# Foreach ($ customer in $ customerlist)

<Tr> <TD> $ velocitycount </TD> <TD> $ customer. Name </TD> </tr>

# End

</Table>

The reference variable of the default number of cycles is $ velocitycount. You can modify it in the configuration file velocity. properties as follows:

# 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

Note: you can add a maximum value to the number of cycles. The default value is-1, indicating the RMB limit:

# The maximum allowed number of loops.

Directive. foreach. maxloops =-1

4. # include-introduce a local file into the template without using velocity to parse the file

Format:

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

  • ARG-The valid file name under the directory template_root.

Examples:

  • Directly write the file name: # include ("disclaimer.txt," opinion.txt "): Separate multiple files by commas.
  • File Name referenced by the variable: # include ($ Foo, $ bar)

5. # parse-use velocity in template reference to parse the output of another template

Format:

#[{]Parse[}](ARG)

  • ARG-The valid file name under the directory template_root.

Examples:

  • Directly write the file name: # parse ("lecorbusier. VM ")
  • File Name referenced by the variable: # parse ($ Foo)

By setting the maximum value of the resolution level depth in the configuration in velocity. PropertiesParse_directive.maxdepthIn can prevent endless loops. (the default parse depth is 10 .)

6. # Stop-interrupt template Parsing

Format:

#[{]Stop[}]

Usage:

Stop parsing in the current template command for convenience of debugging.

7. # macro-allows users to define macro operations (velocimacro (VM): A group of VTL that implement specific functions)

Format:

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

  • Vmname-Macro name VM (# Vmname)
  • $ Arg1 $ arg2 [...]-The VM parameter to be passed to the macro ..
  • [Vm vtl code...]-Macro code, valid VTL.

Once defined, macro commands can be used anywhere in Other templates.

# Vmname ($ arg1 $ arg2)

Macro (VM) can be written in the following two places:

1.(Template Library) Template Library:You can configure user-defined libraries for full-site use.

2.Inline:Put in a general template file, only when the configuration parametersVelocimacro. permissions. allowinline = true.

4. Comments Annotation

Comments is not required for running, but you must write it.

1. Single Row Annotation

Example:

# This is a comment.

2. multiline Annotation

Example:

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

# C # column

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.