The Template Engine

Smart Sites Template Engine is the Core of Smart Sites.
The core engine is base upon Apache Velocity implementation. Smart Sites (and BEEing Platform) adds to Velocity Engine some custom features and behaviour.

However, most of template engine code is written in Velocity template language.
For detailed documentation about Velocity template language you can refer to Official Velocity Documentation.

If you prefer a PDF format of Velocity Documentation, you can download from here.

Quick Introduction to Velocity Template Language

Velocity make it easy to customize HTML pages simply adding few lines of script code (like in PHP).
Statements begins with # simbol and variables or Java objects can be accessed using $.

The Velocity Template Language (VTL) is meant to provide the easiest, simplest, and cleanest way to incorporate dynamic content in a web page. Even a web page developer with little or no programming experience should soon be capable of using VTL to incorporate dynamic content in a web site.

Variable declaration

VTL. Variable declaration

This example set a variable named “a” ($a) with value “Velocity”.
The # character is followed by a directive, set. The set directive uses an expression (enclosed in brackets).
The variable is listed on the left hand side and its value on the right hand side; the two are separated by an = character.

Variables, like all references to Java Objects, begins with $ character.

Quoting Strings.

String values are always enclosed in quotes, either single or double quotes.
Both this are valid Strings: “This is a double quoted String”, ‘This is a single quoted String’.
Difference between double quoting and single quoting is that:

  • Double Quote allow you to use references to interpolate, such as “Hello $name”, where $name will be replaced with value of “name” variable. If $name equals “Angelo”, the output will be “Hello Angelo”.
  • Single Quote ensure that the quoted value will be assigned to the reference as is. So, output of ‘Hello $name’ (with single quotes) will be “Hello $name”.

References and Directives.

References and Directives are main actors of VTL programming.

The following rule of thumb may be useful to better understand how Velocity works: References begin with $ and are used to get something. Directives begin with # and are used to do something.

It’s easy to remember:

  • $ get something
  • # do something

VTL. Using Directives and References

The output of this template is an HTML page that prints “Hello Velocity World!”.

Comments: Single-Line, Multi-Line and Blocks.

“Comments will make your world better.” (This is not only my opinion).
So, remember to use comments and to write useful things.

The ## characters are used for a single line comment.

## This is a single line comment!

Multi-Line comments begins with #* and ends with *#.

#*
This is a multi-line comment.
Remember that comments can help you and other developers to understand better
code you wrote.
*#

The third type of comment il VTL block. In a VTL block you can store any sort of extra information like author or version.

VTL. Comments

NOTE: Don’t worry about comments size because it will not affect output dimension. Velocity Template Engine removes all comments from generated output.