A minimal template would look like this.
Rendering it with
templateEngine.render("example.jte", null, output); will return
To display data in a template, wrap it in
If your model class would look like this:
The output of the above template would be
jte provides convenient shortcuts for common Java control structures, such as conditional statements and loops. These shortcuts provide a clean, terse way of working with control structures while remaining familiar to their Java counterparts.
You may construct if statements using the keywords
@endif. These translate directly to their Java counterparts:
Since Java 14+, you can also use Pattern Matching for
In addition to if statements, jte provides the
@endfor keywords to loop over iterable data. Again,
@for translates directly to its Java or Kotlin counterpart:
When looping, you may use the
gg.jte.support.ForSupport class to gain information about the loop, such as whether you are in the first or last iteration through the loop.
Since jte 3.0, it is possible to use
@else content renders in case no elements were iterated over in the loop. This is useful for displaying an empty list state without an additional
@if. For example:
jte allows you to define comments in your templates.
jte comments are not included in the output of your template.
To share common functionality between templates, you can call other templates. All templates must be located within the jte root directory.
Here is an example template, located in
Template calls are similar to regular Java methods.
Subdirectories in the jte root directory act like packages in Java. For instance, if the
drawEntry template was located in
my/entry/drawEntry.jte, you would call it like this:
If you don't want to depend on the parameter order, you can explicitly name parameters when calling the template.
Named parameters is what the IntelliJ plugin suggests by default.
You can also define default values for all parameters so they only need to be passed when needed.
The second call could then be simplified to this:
The last parameter of a template can be a varargs parameter. For instance, if you created a tag to wrap elements in a list, you could create a template such as
And call it like this:
gg.jte.Content is a special parameter type to pass template code to other templates, much like lambdas in Java. They are handy for sharing structures between different templates.
Here is an example layout with a content block:
The shorthand to create content blocks within jte templates is an
@ followed by two backticks. Let's call the layout we just created and pass a page content and footer:
Declare local variables like this:
Local variables translate 1:1 to Java code.