Javalin rendering
The javalin-rendering module provides optional template engine support for Javalin.
Each template engine has its own artifact that bundles the engine dependency, so you only
need to add one dependency to get started.
Adding dependencies
Pick the artifact for the template engine you want to use:
- Maven
- Gradle
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin-rendering-{engine}</artifactId>
<version>7.0.1</version>
</dependency>
implementation("io.javalin:javalin-rendering-{engine}:7.0.1")
Available modules
Replace {engine} with the name of the template engine you want to use:
javalin-rendering-commonmark |
➜ https://github.com/commonmark/commonmark-java |
javalin-rendering-freemarker |
➜ https://freemarker.apache.org |
javalin-rendering-handlebars |
➜ https://github.com/jknack/handlebars.java |
javalin-rendering-jte |
➜ https://jte.gg/ |
javalin-rendering-mustache |
➜ https://github.com/spullara/mustache.java |
javalin-rendering-pebble |
➜ https://pebbletemplates.io/ |
javalin-rendering-thymeleaf |
➜ https://www.thymeleaf.org/ |
javalin-rendering-velocity |
➜ https://velocity.apache.org/ |
Using the plugin
All the template engines look for templates/markdown files in src/resources/templates.
To enable a template engine, you have to register it on the Javalin config:
- Java
- Kotlin
Javalin.create(config -> {
config.fileRenderer(new JavalinMustache());
});
Javalin.create { config ->
config.fileRenderer(JavalinMustache())
}
You can also register your own rendering engine.
Rendering a template
Once you have added the dependencies for the template engine you want to use,
all you have to do is call ctx.render() with the path to your template file (and optionally a model):
- Java
- Kotlin
ctx.render("/templateFile.ext", model("firstName", "John", "lastName", "Doe"));
ctx.render("/templateFile.ext", mapOf("firstName" to "John", "lastName" to "Doe"))
Configuring a template engine
If you wish to configure a template engine (for example, to set a root directory for your template files), all constructors have optional parameters with their template engine configurations:
- Java
- Kotlin
Javalin.create(config -> {
config.fileRenderer(new JavalinVelocity(myVelocityEngine));
});
Javalin.create { config ->
config.fileRenderer((JavalinVelocity(myVelocityEngine)
}
The configuration classes are not from Javalin, but from the template engine you are using, so please consult the documentation for that particular template engine to learn how to use them.