Skip to content

Template Engines

Robin Drew edited this page Apr 3, 2025 · 12 revisions

A template engine allows a developer to apply dynamic data to text templates to generate documents like HTML.

Apache Velocity

The first step to using Apache Velocity is to configure the template engine.

// Create the templating engine
VelocityEngine engine = new VelocityEngine();

// Basic properties
engine.setProperty("input.encoding", "UTF-8"); // This is default
engine.setProperty("directive.foreach.maxloops", 1000);

// Loading templates from files ...
// A list of paths on which to locate template files
engine.setProperty("file.resource.loader.path", pathList);
// Disable caching to be able to edit/reload templates while application is running (useful while developing app)
engine.setProperty("file.resource.loader.cache", false);

engine.init();

Once the engine is ready, we can write code to load templates and apply data to them.

// Load the template by its path
Template template = engine.getTemplate("velocity/Test.template", "UTF-8");

// Populate a context with data to be applied to the template
VelocityContext context = new VelocityContext();
context.put("message", "Hello World");

// Merge the template with context and provide a Writer for the output
Writer writer = new StringWriter();
template.merge(context, writer);

Useful links:

Apache Freemarker

The first step to using Apache Freemarker is to build the template configuration.

// Create the templating configuration
Configuration config = new Configuration(Configuration.VERSION_2_3_34);

// Loading templates from files ...
File templateDir = ...
config.setDirectoryForTemplateLoading(templateDir);

// Which exception handler?
// Safe for production
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
// Useful for development
config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER );

// Other userful settings
config.setDefaultEncoding("UTF-8");
config.setLogTemplateExceptions(false);
config.setWrapUncheckedExceptions(true);
config.setFallbackOnNullLoopVariable(false);

Once the configuration is ready, we can write code to load templates and apply data to them.

// Load the template by its path
Template template = config.getTemplate("freemarker/Test.template");

// Populate data in a map to be applied to the template
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("message", "Hello World");

// Merge the template with data and provide a Writer for the output
Writer writer = new StringWriter();
template.process(dataMap, writer);

Useful links:

Thymeleaf

The first step to using Thymeleaf is to build the template engine.

// Create a resolver to locate templates
FileTemplateResolver resolver = new FileTemplateResolver();
resolver.setCacheable(caching);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setPrefix(path);

// Build the engine
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(resolver);

Once the engine is ready, we can write code to load templates and apply data to them.

// Populate data in a map to be applied to the template
Context context = new Context();
context.setVariable("message", "Hello World");

// Merge the template with data and provide a Writer for the output
Writer writer = new StringWriter();
engine.process(name, context, writer);

Language Examples

<!-- Text -->
<title th:text="${page.title}" />

<!-- Foreach Loop -->
<ul th:each="student: ${students}">
   <li><span th:text="${student.name}"></li>
</ul>

<!-- Switch Statement -->
<div th:switch="${time.unit}">
   <span th:case="'MINUTES'" th:text="Minutes" /> 
   <span th:case="'HOURS'"   th:text="Hours" /> 
</div>

<!-- If Unless Statement -->
<td>
    <span th:if="${student.gender} == 'M'"     th:text="Male" /> 
    <span th:unless="${student.gender} == 'M'" th:text="Female" />
</td>

<!-- Selection (when using * this is a field of the selected object) -->
<div th:object="${book}">
  <span th:text="*{title}"></span>
</div>

Useful links:

Clone this wiki locally