Templates syntax¶
A minimal template would look like this.
Rendering it with templateEngine.render("example.jte", null, output);
will return Hello world!
.
Displaying data¶
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 Hello jte!
.
${}
can be used to output the following types:
String
Enum
boolean
,byte
,short
,int
,long
,float
,double
,char
- Any class implementing
gg.jte.Content
For security reasons there is no automatic .toString()
conversion. Unsupported types produce a compilation error.
Control structures¶
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.
If Statements¶
You may construct if statements using the keywords @if
, @elseif
, @else
and @endif
. These translate directly to their Java counterparts:
Tip
Since Java 14+, you can also use Pattern Matching for instanceof
:
Loops¶
In addition to if statements, jte provides the @for
and @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
before @endfor
. The @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:
Comments¶
jte allows you to define comments in your templates.
Info
jte comments are not included in the output of your template.
Template Calls¶
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 my/drawEntry.jte
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:
Named parameters¶
If you don't want to depend on the parameter order, you can explicitly name parameters when calling the template.
Note
Named parameters is what the IntelliJ plugin suggests by default.
Default parameters¶
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:
Varargs¶
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 list.jte
:
And call it like this:
Content¶
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:
Variables¶
Declare local variables like this:
Local variables translate 1:1 to Java code.