Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 7
The Velocity Template Language (VTL) is a simple syntax providing two parts: references, a formalism for accessing objects in the context; and directives, a set of statements used for control and action. Described as "a language definition with a feature set that fits comfortably on a standard business card" (see Jim Jagielski's "Getting Up to Speed with Velocity") VTL has been intentionally kept simple and small by the community.
References in the template access data. They freely mix with the template's non-VTL content. Formally defined, a reference is anything in a template that starts with the '$' character and refers to something in the context. If no corresponding data object exists in the context, the template simply treats the reference as text and renders it as-is into the output stream.
Here is a short template containing a simple reference mixed with non-VTL content:
Hello $name! Welcome to Velocity!
Here, the reference is $name. As in the Hello World example, Velocity replaces $name in the template with the toString() return value of what is placed in the context under the key name:
Hello World! Welcome to Velocity!
The Velocity reference allows access to any object's public method, and the template's syntax is the same as it would be in Java code. Here are a few examples:
There are $myBean.getSize() elements.
$myObject.anotherMethod( 1, "more data ")
$foo.getBar().barMethod("hello", $moredata )
$foo.myMethod( $bar.callThis() )
You may recall from the Pet Store email example that we stored the name and price information in a java.util.Map, and accessed the data using two tokens name and price, which don't exist as methods in the java.util.Map class:
$pet.name for only $pet.price
This works because Velocity incorporates a JavaBean-like introspection mechanism that lets you express method accesses in
references using a property notation. In the Pet Store example template, Velocity's introspection facility finds and invokes
the Map's public Object get(String) method with the keys name and price. We could access the same data in a different way by invoking the get(String) method directly in the template:
$pet.get('name') for only $pet.get('price')
This would produce the same output, and better represents what is actually happening. However, the other way that uses the
property notation is easier to read and doesn't tie your template to the data class's specific implementation. For example,
you can replace the Map in the List with a class that has public methods getName() and getPrice(), and the original example template containing the following will continue to work:
$pet.name for only $pet.price
This is one way Velocity lets you decouple the view from your application's model-controller portion. Velocity's support for data access, with references, is a powerful and flexible facility. Your template references are only required to correspond to publicly accessible methods or be accessible through Velocity's property formalism.