Skip to content

Templates

Module port_templates

This port provides a common interface for rendering templates with multiple different template engines.

Install the Dependency

This module is not meant to be used directly. You should include any Adapter implementing this feature (as templates_pebble and/or templates_freemarker) in order to process templates.

You can use many adapters in the same application to be able to handle different template engines at the same time.

Package com.hexagonkt.templates

Create a Template Engine

Settings

Template engines can be configured:

Usage

To render a template, do something like this:

1
2
3
val context = mapOf("key1" to "value1", "key2" to "value2")
val locale = Locale.getDefault()
val rendered = engine.render(resource, context, locale)

Using multiple template engines

To make the use of multiple template engines more convenient, you can use the TemplateManager. Just register multiple template engines (or the same engine with different configurations) under a prefix and use it like follows:

1
2
3
4
5
6
7
TemplateManager.adapters = mapOf(
    Regex(".*\\.html") to TestTemplateAdapter("html"),
    Regex(".*\\.txt") to TestTemplateAdapter("text")
)

val html = TemplateManager.render("template.html", context, locale)
val plain = TemplateManager.render("template.txt", context, locale)