== jte

https://jte.gg[Jte] Secure and speedy templates for Java and Kotlin.

=== Usage

1) Add the dependency:

[dependency, artifactId="jooby-jte"]
.

2) Configure code generator

.Maven
[source,xml,role="primary",subs="verbatim,attributes"]
----
<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>
----

.Gradle
[source,groovy,role="secondary",subs="verbatim,attributes"]
----
plugins {
    id 'java'
    id 'gg.jte.gradle' version '{jte_version}'
}

dependencies {
    implementation('gg.jte:jte:{jte_version}')
}

jte {
    generate()
}
----

NOTE: Complete code generator options are https://github.com/casid/jte/blob/main/DOCUMENTATION.md#precompiling-templates[available here].

3) Write your templates inside the `src/main/jte` folder

.src/main/jte/hello.jte
[source, html]
----
@param String name

<p>Hello ${name}!</p>
----

4) Install and use jte templates

.Java
[source, java, role="primary"]
----
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>
  });
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jte.JteModule

{
  install(JteModule(Paths.get("src", "main", "jte")))              <1>

  get("/") {
    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.

=== 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", artifactId="jte-models", version="jte.version"]
.

2) Configure code generator

.Maven
[source,xml,role="primary",subs="verbatim,attributes"]
----
<plugin>
    <groupId>gg.jte</groupId>
    <artifactId>jte-maven-plugin</artifactId>
    <version>{jte_version}</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>{jte_version}</version>
        </dependency>
    </dependencies>
</plugin>
----

.Gradle
[source,groovy,role="secondary",subs="verbatim,attributes"]
----
plugins {
    id 'gg.jte.gradle' version '{jte_version}'
}

dependencies {
    implementation 'gg.jte:jte-runtime:{jte_version}'
    jteGenerate 'gg.jte:jte-models:{jte_version}'
}

jte {
    generate()
    binaryStaticContent = true
    jteExtension 'gg.jte.models.generator.ModelExtension'
}
----

==== Usage

.Java
[source,java,role="primary"]
----
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!");
  });
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jte.JteModule

{
  install(JteModule(Paths.get("src", "main", "jte")))

  get("/static") {
    val templates = StaticTemplates()
    templates.helloWorld("Hi!")
  }

  get("/dynamic") {
    val templates = DynamicTemplates(require(TemplateEngine::class))
    templates.helloWorld("Hi!")
  }
}
----

More at https://jte.gg/jte-models/[jte-models].

=== Options

==== 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:

.Maven
[source,xml,role="primary",subs="verbatim,attributes"]
----
<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>
----

.Gradle
[source,groovy,role="secondary",subs="verbatim,attributes"]
----
plugins {
    id 'java'
    id 'gg.jte.gradle' version '${jte.version}'
}

dependencies {
    implementation('gg.jte:jte:${jte.version}')
}

jte {
    generate()
}
----

You need to make sure to copy the `compiled-templates` folder as part of your deployment process. See https://github.com/casid/jte/blob/main/DOCUMENTATION.md#using-a-directory-on-your-server-recommended[using a directory on your server] for more details.

==== Custom Engine

It is possible to provide your own/custom template engine:

[source,java]
----
    var templateEngine = TemplateEngine.create(...) or TemplateEngine.createPrecompiled(..)
    install(new JteModule(templateEngine));
----
