Skip to content

[CORE 03] Template Renderer

Elmar Dott edited this page Aug 30, 2021 · 2 revisions

TemplateRenderer

@since: 1.0 > @api-version: 1.0 > Dependencies: Apache Velocity

Files:


The velocity project was chosen for the template engine. The important arguments for this choice was an good documentation, plenty functionality, an active community and a successful usage in many other projects. Also very important ist the fact that the usage is very simple.

Template content can be loaded from a Classpath resource, from the File System or from a String. Specially the last option to load content from a String gives a high flexibility to handle other Resource types like databases or micro services in a general manner.

Beside the usage of variables, Velocity supports a full scripting language to develop complex templates for a generic usage.

Sample:

TemplateRenderer velocityRenderer =
                        new VelocityRenderer();

String resourcePath = "org/europa/together/velocity";
String template = "template.vm";

Map<String, String> model = new HashMap<>();
model.put("property_key", "value");

String content = velocityRenderer
            .generateContentByClasspathResource(
                resourcePath, template, model);

Template:

## single line comment 
#set( $var = "Velocity" )
Hello $var World?
#if($property_key=="value") : $property_key #endy

Output: Hello Velocity World? value.

The HashTable demonstrate how simple it is to use variables inside the template. A simple if condition is set in the example, but the processor is much more powerful an support other things like loops.

The full processed template is placed inside the String content and can be used to write inside a file or displayed in a HTML page. Other usages can be E-Mail templates to create dynamic mails, send by the SMTP E-Mail Client [TP-0006]. To use the full advantage of Velocity templates check the project documentation.