Velocity Templates are a powerful templating engine that allows outputting custom content in your format with most basic case – HTML. I’ve written very basic example with Spring Boot. Here are some everyday features of Velocity templates that you could use:
Variables
You can define variables in the template and use them throughout the page.
#set($name = "John")
Hello, $name!
Loops
Velocity supports various types of loops, such as #foreach
for iterating over arrays or collections.
#foreach($item in $items)
* $item
#end
Conditional statements
You can use #if
to check conditions and display different content based on the result.
#if($isAdmin)
You are an administrator.
#else
You are not an administrator.
#end
Functions
Velocity allows you to define custom functions that can be used within templates.
#define(greet $name)
Hello, $name!
#end
${greet("John")}
Include directives
You can include other Velocity templates using the #include
directive, making it easy to reuse code.
#include("header.vm")
<!-- content here -->
#include("footer.vm")
Escape sequences
Velocity supports various escape sequences for displaying special characters, such as #lt
for less than and #gt
for greater than.
Hello, <b>John</b>!
URL encoding
You can use the $esc.html()
function to URL encode strings, making it easier to pass data in URLs.
<a href="http://example.com?name=${esc.html("John")}">Click me!</a>
Date and time formatting
Velocity itself does not have built-in date formatting, but you can format dates using SimpleDateFormat
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd")
String formattedDate = formatter.format(new Date());
Math operations
You can perform basic math operations, such as addition and multiplication, using the $num
function.
The sum of 2 and 3 is: ${num.add(2, 3)}
String manipulation
Velocity offers various string functions, such as trim()
and replace()
, for manipulating strings.
The trimmed name is: ${esc.html(" John ").trim()}
Array manipulation
You can use Velocity’s array functions, such as sort()
and get()
, to manipulate arrays.
The sorted array is: ${array.sort([3, 1, 2])}
Object manipulation
Velocity provides various object functions, such as get()
and set()
, for manipulating objects.
The value of the "name" property is: ${obj.get("name")}
Custom tags
You can define custom tags using the #tag
directive, making it easy to create reusable UI components.
#tag(name="greeting", body="Hello, $name!")
${greeting("John")}
Velocity macros
Velocity allows you to define custom macros that can be used within templates, making it easy to reuse code.
#define(greet $name)
Hello, $name!
#end
${greet("John")}
Error handling
Velocity provides various error-handling mechanisms, such as the #error
directive and the $esc.html()
function, to help you handle errors.
#if($error)
An error occurred: ${esc.html($error)}
#else
No errors occurred.
#end
These are just a few examples of the many features available in Velocity templates. By mastering these features, you can create powerful and flexible web applications.