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.\n\n\nHere is a short template containing a simple reference mixed with non-VTL content:\n\n Hello $name! Welcome to Velocity!\n\n\nHere, 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:\n\n Hello World! Welcome to Velocity!\n\n\nThe 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:\n\n There are $myBean.getSize() elements.\n $myObject.anotherMethod( 1, "more data ")\n \n $foo.getBar().barMethod("hello", $moredata )\n \n $foo.myMethod( $bar.callThis() )\n\n\nYou 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:\n\n $pet.name for only $pet.price\n\n\nThis 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:\n\n $pet.get('name') for only $pet.get('price')\n\n\nThis 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:\n\n $pet.name for only $pet.price\n\n\nThis 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.\n\nDirectives\n\nDirectives are VTL's other major part. Like references, they freely mix with other non-VTL content in templates. The directives in VTL are:\n\n #set()\n #if()\n #else\n #elseif()\n #end\n #foreach()\n #include()\n #parse()\n #macro()\n\n\nThe #set() directive lets you set a reference value within the template. This can replace an existing object in the context or establish a new one. Useful to the designer for presentation logic, #set() allows common mathematical operations on integers (+,-,\/,*,%), as well as Boolean evaluations:\n\n #set( $startrow = 0)\n #set( $count = $current + 1 )\n #set( $isReady = ( $isOn && $isOpen) )\n\n\nThe #if() #else #elseif() #end set of directives provides the usual if\/then\/else functionality common in many programming and scripting languages. The #if() and #elseif() take an expression that can use the common logical operators &&, ||, !, ==, >, >=,","wordCount":2218,"datePublished":"2001-12-28T03:00:00-05:00","dateModified":"2001-12-28T03:00:00-05:00","keywords":"Java,Development Tools,Web Development,Software Development","mainEntityOfPage":"https:\/\/www.infoworld.com\/article\/2161335\/start-up-the-velocity-template-engine.html"}]