VTL documentation

About this guide                                                                                                              

This guide is for the Velocity Template Language (VTL) explained. More additional information, please refer to Velocity User Guide ( http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html ) .

 

Explanation                                                                                                                          

variable

expression:

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

 

Example:

  • Shorthand expression: $ MUD-Slinger_9
  • Shorthand expression: $ MUD-Slinger_9!
  • Regular expressions: $ {} MUD-Slinger_9
  • Regular expressions: $ MUD-Slinger_9} {!

 

Attributes

expression:

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

 

Example:

  • Legal expression: $ customer.Address
  • Regular expression: $ {} purchase.Total

 

method

expression:

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

 

Example:

  • Legal expression: $ customer.getAddress ()
  • Regular expression: $ {purchase.getTotal ()}
  • Legitimate expression with a list of parameters: $ page.setTitle ( "My Home Page")

 

VTL properties can be taken out set and get a shorthand way of expression, $ object.getMethod () or $ object.setMethod () can be abbreviated to $ Object.Method case, allowed, generally prefer written expression properties type way. The main difference lies in the method attributes and methods can have a list of arguments.

 

command                                                                                                                          

#set - Establishes the value of a reference

format:

# [ { ] set [ } ] ( $ref = [ ", ' ]arg[ ", ' ] )

 

use:

  • $ref -
  • arg -

 

Example:

  • Variables: #set ($ = $ Monkey Bill)
  • String: #set (monkey.Friend $ = 'Monica')
  • 属性:#set($monkey.Blame=$whitehouse.Leak)
  • 方法:#set( $monkey.Plan = $spindoctor.weave($web))
  • Digital: #set ($ monkey.Number = 123)
  • Operating range: #set ($ monkey.Numbers = [1..3])
  • 列表:#set( $monkey.Say = ["Not", $my, "fault"] )
  • Map#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"})

 

RHS can also be an expression, for example:

  • Addition: #set (value = $ + $ foo. 1)
  • Subtraction: #set ($ value = $ bar - 1)
  • Multiplication: #set ($ value = $ * $ foo bar)
  • Division: #set ($ value = $ foo / bar $)
  • 取余:#set( $value = $foo % $bar )

 

#if/#elseif/#else - Output conditional on truth of statements

format:

# [ { ] if [ } ] ( [condition] ) [output] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]

 

use:

  • Condition: If a boolean type Considered A true IF IT has true to false ; if not boolean type, if not null was true
  • Output: contains VTL

 

Example:

Operation name

symbol

Optional Symbols

Examples

Equal numbers

==

eq

#if($foo == 42)

Character is equal to

==

eq

#if($foo == 'bar')

Objects are equal

==

eq

#if($foo == $bar)

Unequal

!=

born

#if($foo != $bar)

more than the

>

gt

#if($foo > 42)

Less than

<

lt

#if($foo < 42)

greater or equal to

>=

give

#if($foo >= 42)

Less than or equal

<=

the

#if($foo <= 42)

boolean negated

!

not

#if(!$foo)

 

note:

  1. == can be used to compare numbers, strings and objects are equal, the last example ( the object is not of the same class ) , toString () String method is called, and then out to compare
  2. You can use parentheses to separate them when behind the text followed by # when this is useful.

#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 $  - circulation list first variable reference.
  • Arg  - it can make a list of one kind of reference ( an array of objects, set or the Map) , one kind of a list or an array operation interval.
  • of Statement  - When Velocity in the list arg time to find a legitimate target, it will output a legitimate VTL .

 

Example:

  • Quote: # Foreach ($ Item in $ items)
  • Array list: # Foreach ($ Item in [ "Not", $ My, "Fault"])
  • Operating range: #foreach ($ in Item [1..3])

 

Velocity provides an easy way to get the number of cycles:

<table>
#foreach( $customer in $customerList )
    <tr><td>$foreach.count</td><td>$customer.Name</td></tr>
#end
</table>

Further, in Velocity1.5 allowed maximum number of cycles can be controlled. The default 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  - TEMPLATE_ROOT a legal document introduced next.

 

Example:

  • String#include( "disclaimer.txt" "opinion.txt" )
  • Variable: #include ($ foo $ bar)

 

#parse - Renders a local template that is parsed by Velocity

format:

# [ { ] parse [ } ] ( arg )
  • Arg  - TEMPLATE_ROOT template file under.

 

Example:

  • String #parse( "lecorbusier.vm" )
  • Variables: #parse ($ foo)

 

Recursion is permitted, you can look velocity.properties in parse_directive.maxdepth to modify the parse depth (default parse depth is 10 ).

 

#stop - Stops the template engine

format:

# [ { ] stop [ } ]

 

scenes to be used:

Stop the current template, in debug when the template is relatively easy to use.

 

#break - Stops the current directive

format:

# [ { ] break [ } ]

 

Usage scenarios: the current contents of the instruction to terminate early in #foreach is used to advance the end of the cycle, it may be still other range. You can even skip the special range outside range.

 

#evaluate - Dynamically evaluates a string or reference

format:

# [ { ] evaluate [ } ] ( arg )
  • arg - String literal or reference to be dynamically evaluated.

 

Example:

  • 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  - calling VM when naming
  • arg1 $ arg2 $ [...]  - VM parameters. There may be a plurality of parameters, but the parameters must match the definition.
  • [VM VTL code ...]  - Any valid VTL code that can be placed into a template, or you can put VM in.

 

Once defined, VM like other VTL instructions as to use.

#vmname( $arg1 $arg2 )

In addition, when you body wants to call vm , you must vm add front @ by $ bodyContent! , Body will find references to the macro definition.

#@vmname( $arg1 $arg2 ) here is the body#end

VMs can be 2 places Definition:

  1. Template library: can be from any pre-packaged templates or custom, user-defined, custom website
  2. Inline: In velocity.properties only when the velocitymarco.permissions.allowInline = true time, then you can use the official templates.

 

Note                                                                                                                          

Comments are not rendered at runtime.

 

Single-line comments

## This is a comment.

 

Multi-line comments

#*

This is a multiline comment.

This is the second line

*#

Not parsed content                                                                                                           

The content is not resolved at run-time rendering, rather than no resolution.

 

#[[

This has invalid syntax that would normally need "poor man's escaping" like:

  • #define()
  • ${blah

]]#

Reproduced in: https: //my.oschina.net/secyaher/blog/274481

Guess you like

Origin blog.csdn.net/weixin_33672400/article/details/91966887