1. jte
Jte Secure and speedy templates for Java and Kotlin.
1.1. Usage
1) Add the dependency:
Maven
Gradle
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jte</artifactId>
<version>3.5.3</version>
</dependency>
2) Configure code generator
Maven
Gradle
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>${jte.version}</version>
<configuration>
<sourceDirectory>${basedir}/src/main/jte</sourceDirectory> <!-- This is the directory where your .jte files are located. -->
<contentType>Html</contentType>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Complete code generator options are available here. |
3) Write your templates inside the src/main/jte
folder
src/main/jte/hello.jte
@param String name
<p>Hello ${name}!</p>
4) Install and use jte templates
Java
Kotlin
import io.jooby.jte.JteModule;
{
install(new JteModule(Paths.of("src", "main", "jte"))); (1)
get("/", ctx -> {
return new ModelAndView("hello.jte", Map.of("name", "Jte")); (2)
});
}
1 | Install Jte Module |
2 | Returns a model and view |
Jte is configured to in development mode when application.env
is set to dev
or test
. Jte
will put all the generated classes in src/main/jte/jte-classes
.
In production will read the classes from classpath.
1.2. Options
1.2.1. Custom class directory
If you prefer a custom directory for compiled templates you need to do use:
install(new JteModule(Paths.of("src", "main", "jte"), Paths.of("compiled-templates")));
Also, you need to configure Maven or Gradle to generate templates classes:
Maven
Gradle
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>${jte.version}</version>
<configuration>
<sourceDirectory>src/main/jte</sourceDirectory> <!-- This is the directory where your .jte files are located. -->
<targetDirectory>compiled-templates</targetDirectory> <!-- This is the directory where compiled templates are located. -->
<contentType>Html</contentType>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>precompile</goal>
</goals>
</execution>
</executions>
</plugin>
You need to make sure to copy the compiled-templates
folder as part of your deployment process. See using a directory on your server for more details.
1.2.2. Custom Engine
It is possible to provide your own/custom template engine:
var templateEngine = TemplateEngine.create(...) or TemplateEngine.createPrecompiled(..) install(new JteModule(templateEngine));