Overview of template engine features
On the first of December 2008, Apache announced the release of a new version of the not yet updated template engine Velocity, numbered 1.6, and two weeks later an update with fresh fixes appeared, Velocity Engine 1.6.1. Those who use Velocity in their projects will be happy to learn about new features introduced in the new version. Those who do not use Velocity, perhaps, will discover a new useful tool.
Quick reference
Apache Velocity is a software product consisting of a number of Java libraries. The product is developed and supported as part of the Apache Software Foundation, and is distributed as open source software under the
Apache License 2.0 license. The main component of the product is the Velocity Engine - a library that allows you to generate output texts based on templates, which is commonly referred to as a template engine.
Why do we need template engines
Generally, template engines find the widest use in web development, but the easiest way is to show the benefits of using them on the example of forming the letter text. Suppose we have a user base, and we want to send an announcement of a new product. The text is standard, changing only the name, date and name of the product. Open the notebook and write:
$customer.name
" " , $product.name ! .
$date
" "
As you have already guessed, $ customer.name, $ product.name and $ date are variables, instead of which real values ​​will be substituted for the formation of the output text. The question arises: how to transfer these values ​​to the pattern? This is done very simply. In the java source we prescribe:
')
VelocityEngine velocity = new VelocityEngine();
velocity.init();
Template letter = velocity.getTemplate("letter.vm");
Date now = new Date();
Product product = someDAO.getLatestProduct();
List<Customer> customers = someDAO.getCustomers();
for (Customer customer : customers) {
VelocityContext vc = new VelocityContext();
vc.put("customer", customer);
vc.put("product", product);
vc.put("date", now);
StringWriter sw = new StringWriter();
letter.merge(vc, sw);
//
As you can see, there is nothing really tricky here: Java objects, each under their own key, are laid into the VelocityContext instance, which in essence is a regular Map. When in the process of merging a context with a template, the engine encounters a variable in the text, it uses its name as a key, and extracts the corresponding object from it. Telemarket!
Of course, there are no problems to adapt the templates for generating HTML pages, which many successfully did at one time, throwing the JSP hemorrhoids technology into the dustbin of history.
A detailed description of the VTL template language and instructions for its use can be found on the developer’s website.
But it was all backstory. And now about the new version itself. To the delight of developers, in 1.6 a number of long-awaited goodies appeared. Consider them in order.
1. Directive #evaluate
Here is a simple and vital example: let's say the article text is stored in the database. Somewhere in the middle we want to give a sidebar with some live data: let's say, with the results of Internet voting. It is not difficult: just in the right place to connect the template file, which displays the results in the form of HTML:
#parse("poll.vm")
The trouble, however, is that if we put the text of the article as a regular variable ($ article.text), its contents will be stupidly displayed on the page as it is. That is, instead of a beautiful chart, the reader will see the unintelligible #parse (“poll.vm”). In order to achieve the desired effect, we need to somehow force Velocity to interpret the text of the article as a template code. One of the few opportunities to do this in previous versions was to use the additional RenderTool component from the Velocity Toolbox. That was, in principle, tolerable, but somewhat inconvenient. With the release of 1.6, we were able to do it in one line:
#evaluate ($article.text)
2. Directive #define
This directive works much like a macro, with the difference that pieces of the template code defined with it can not only be called locally, but also assigned to the context variable and then used where they will be needed:
#define($block)Hello $who#end
#set($who = 'World!')
$block
Do we need such a construction in practice? Not so much, but with it, some things can be implemented in a more natural way. For example, something like template polymorphism.
3. Directive #break
Trifle, but nice: now you can jump out of the #foreach cycle in one line. With the meager means that Velocity had for expressing control logic, the addition of a whole new directive is already a huge breakthrough.
4. Ability to organize macros in files
One of the annoying limitations of the old Velocity was the rigidly fixed location of the macro file. Now macros can be organized at your discretion, and connect them as needed:
#parse('mymacros.vm')
Instead of conclusion
In this review, the most interesting innovations that appeared in version 1.6 of the Velocity Engine were reviewed. More details about the new features of the engine can be found on the product site in the
Changes section.
Well, summing up, I can not fail to note: “Life has become better, life has become more fun”!
