1. jte
Jte Secure and speedy templates for Java and Kotlin.
1.1. Usage
1) Add the dependency:
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jte</artifactId>
<version>3.6.0</version>
</dependency>
2) Configure code generator
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>3.1.15</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
@param String name
<p>Hello ${name}!</p>
4) Install and use jte templates
import io.jooby.jte.JteModule;
{
install(new JteModule(Paths.get("src", "main", "jte"))); (1)
get("/", ctx -> {
return new MapModelAndView("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. Models
jte-models is a generator extension for jte that creates a typesafe facade for rendering templates.
1) Add the dependency:
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte-models</artifactId>
<version>3.1.15</version>
</dependency>
2) Configure code generator
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>3.1.15</version>
<configuration>
<sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
<contentType>Html</contentType>
<extensions>
<extension>
<className>gg.jte.models.generator.ModelExtension</className>
</extension>
</extensions>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte-models</artifactId>
<version>3.1.15</version>
</dependency>
</dependencies>
</plugin>
1.2.1. Usage
import io.jooby.jte.JteModule;
{
install(new JteModule(Paths.get("src", "main", "jte")));
get("/static", ctx -> {
var templates = new StaticTemplates();
return templates.helloWorld("Hi!");
});
get("/dynamic", ctx -> {
var templates = new DynamicTemplates(require(TemplateEngine.class));
return templates.helloWorld("Hi!");
});
}
More at jte-models.
1.3. Options
1.3.1. Custom class directory
If you prefer a custom directory for compiled templates you need to do use:
install(new JteModule(Paths.get("src", "main", "jte"), Paths.get("compiled-templates")));
Also, you need to configure Maven or Gradle to generate templates classes:
<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.3.2. Custom Engine
It is possible to provide your own/custom template engine:
var templateEngine = TemplateEngine.create(...) or TemplateEngine.createPrecompiled(..)
install(new JteModule(templateEngine));