Summarized VTL Guide - Precisely_EnterWorks - EnterWorks - 11.0

EnterWorks Guide

Product type
Software
Portfolio
Verify
Product family
EnterWorks
Product
Precisely EnterWorks
Precisely EnterWorks > EnterWorks
Version
11.0
Language
English
Product name
Precisely EnterWorks
Title
EnterWorks Guide
Copyright
2024
First publish date
2007
Last updated
2025-01-07
Published on
2025-01-07T07:44:20.997000

General

Variables

Notation:

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

Examples:

  • Normal notation: $mud-Slinger_9
  • Silent notation: $!mud-Slinger_9
  • 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}
VTL Properties can be used as a shorthand notation for VTL Methods that take get and set. Either $object.getMethod() or $object.setMethod() can be abbreviated as $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.
Methods

Notation:

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

Examples:

  • Regular Notation: $customer.getAddress()
  • Formal Notation: ${purchase.getTotal()}
  • Regular Notation with Parameter List: $page.setTitle( "My Home Page" )

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, arg is parsed if enclosed in double quotes, and not parsed if enclosed in single quotes.

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 )
  • Object array: #set( $monkey.Say = ["Not", $my, "fault"] )

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

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

  • Equivalent Operator: #if( $foo == $bar )
  • Greater Than: #if( $foo > 42 )
  • Less Than: #if( $foo < 42 )
  • Greater Than or Equal To: #if( $foo >= 42 )
  • Less Than or Equal To: #if( $foo <= 42 )
  • Equals Number: #if( $foo = 42 )
  • Equals String: #if( $foo = "bar" )
#foreach - Loops through a list of objects

Format:

#foreach( $ref1 in $ref2 ) [ statement... ] #end

Usage:

  • $ref1 - The first variable reference is the item.
  • $ref2 - The second variable reference is the list that holds the items.
  • statement - What is output each time Velocity finds a valid item ($ref1) in the list ($ref2).

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>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>

The default name for the loop counter variable reference, which is specified in the velocity.properties file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the velocity.properties file. Here's what the loop counter properties section of the velocity.properties file appears:

# Default name of the loop counter
# variable refernce. counter.name = velocityCount
# Default starting value of the loop
# counter variable reference. counter.initial.value = 1
#include - Renders local files 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. See parse_directive.maxdepth in velocity.properties to 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.

#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 argumentss, 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 )

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 when velocimacro.permissions.allowInline=true in velocity.properties.

Comments

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

*#

References:

Ja-Jakarta Project Velocity Users Guide, Copyright © 1999-2002, The Apache Software Foundation

Ja-Jakarta Project VTL Reference, Copyright © 1999-2002, The Apache Software Foundation

EnterWorks Users Guide, Copyright © 2010, EnterWorks Inc.