Java Template Engine [JTE]

The Java Template Engine (jte) is a secure and lightweight template engine designed for Java and Kotlin applications. It emphasizes minimal syntax, leveraging existing language features to make templates intuitive and maintainable.

circle-info

for more info visit official doc - https://jte.ggarrow-up-right

Some of the features of JTE -

  1. Performance: JTE compiles templates to Java bytecode, resulting in efficient runtime execution.

  2. Compile-time checking: Catch errors at compile time rather than runtime.

  3. Lightweight: Minimal overhead and dependencies.

  4. Hot reloading: See changes immediately without restarting your application.

  5. Pre-compilation: Improve startup times by pre-compiling templates.

Integrating jte into a Spring Boot Application

  1. Create spring boot project with web starter.

  2. add the following dependency to use jte

 <dependency>
      <groupId>gg.jte</groupId>
      <artifactId>jte</artifactId>
      <version>3.1.12</version>
 </dependency>
<dependency>
      <groupId>gg.jte</groupId>
      <artifactId>jte-spring-boot-starter-3</artifactId>
      <version>3.1.12</version>
</dependency>

It will also create jte folder in application, in which we need to store our templetes.

  1. Add the following plugin along with spring boot maven plugin

  1. Create a template in jte directory with extension of .jte.

template - index.jte in src/main/jte

  1. To view this on browser we need to create a controller also

Properties available for JTE

Template Syntax

${}

  • To display data from variable , can be used with general data types along with Any class implementing gg.jte.Content

<%-- ... --%>

  • To display comments

@import

  • Used to import direcly from app like classes, records etc.

  • Need to use fully qualified name e.g. @import com.app.Sample

@Param

  • It used to declare the parameter that is passed to this template. e.g. @param Sample sample

  • The datatype could be premitive or the custom dto , models or records etc.

  • For any classes we need to either import them first or need to use fully qualified name with @param.

  • we can also assign default values

@if/@endif

  • Used for flow control in the template

  • for nesting we can also use @elseif

@for and @endfor

  • Used to loop over variable

  • loop can be both fori or for in

  • we can also use @else with for , as in case of no data else part will get executed.

!{}

  • used to declare local variable

  • that can be used with ${}

Content

  • gg.jte.Content is a special parameter type, that is used to pass template code to other templates, much like lambdas in Java.

  • They are handy for sharing structures between different templates.

Example -

example for content -

Last updated