Package org.jusecase.jte
Class TemplateEngine
- java.lang.Object
-
- org.jusecase.jte.TemplateEngine
-
public final class TemplateEngine extends Object
jte is a simple, yet powerful template engine for Java. All jte templates are compiled to Java class files, meaning jte adds essentially zero overhead to your application. jte is designed to introduce as few new keywords as possible and builds upon existing Java features, so that it is very easy to reason about what a template does. Read more at the official documentation at https://github.com/casid/jte/blob/master/DOCUMENTATION.md
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanAll()Cleans the directory containing the compiled template classes.static TemplateEnginecreate(CodeResolver codeResolver)Creates a new template engine.static TemplateEnginecreate(CodeResolver codeResolver, Path classDirectory)Creates a new template engine.static TemplateEnginecreatePrecompiled(Path classDirectory)Creates a new template engine.List<String>getTemplatesUsing(String name)voidprecompileAll()Compiles all templates located in the sources directory, to the compiled template classes directory.voidprecompileAll(List<String> compilePath)Compiles all templates located in the sources directory, to the compiled template classes directory.voidprepareForRendering(String name)Prepares the template with the given name for renderingvoidrender(String name, Object model, TemplateOutput output)Renders the template with the given name.voidsetNullSafeTemplateCode(boolean value)Experimental mode, that ignores anyNullPointerExceptionthat occurs in template files.
-
-
-
Method Detail
-
create
public static TemplateEngine create(CodeResolver codeResolver)
Creates a new template engine. All templates are compiled to Java class files on demand. A JDK is required. Every template has its own class loader. This is recommended when running templates on your developer machine.- Parameters:
codeResolver- to lookup jte templates- Returns:
- a fresh TemplateEngine instance
-
create
public static TemplateEngine create(CodeResolver codeResolver, Path classDirectory)
Creates a new template engine. All templates are compiled to Java class files on demand. A JDK is required. Every template has its own class loader. This is recommended when running templates on your developer machine.- Parameters:
codeResolver- to lookup jte templatesclassDirectory- where template class files are compiled to- Returns:
- a fresh TemplateEngine instance
-
createPrecompiled
public static TemplateEngine createPrecompiled(Path classDirectory)
Creates a new template engine. All templates must have been precompiled to Java class files already. The template engine will load them from the specified classDirectory. No JDK is required. All templates share one class loader with each other. This is recommended when running templates in production. How to precompile templates: https://github.com/casid/jte/blob/master/DOCUMENTATION.md#precompiling-templates- Parameters:
classDirectory- where template class files are located- Returns:
- a fresh TemplateEngine instance
-
render
public void render(String name, Object model, TemplateOutput output) throws TemplateException
Renders the template with the given name.- Parameters:
name- the template name relative to the specified root directory, for instance "pages/welcome.jte".model- the model instance passed to the template.output- any implementation ofTemplateOutput, where the template will be written to.- Throws:
TemplateException- in case the template failed to render, containing information where the error happened.
-
prepareForRendering
public void prepareForRendering(String name)
Prepares the template with the given name for rendering- Parameters:
name- the template name relative to the specified root directory, for instance "pages/welcome.jte".
-
cleanAll
public void cleanAll()
Cleans the directory containing the compiled template classes.
-
precompileAll
public void precompileAll()
Compiles all templates located in the sources directory, to the compiled template classes directory.
-
precompileAll
public void precompileAll(List<String> compilePath)
Compiles all templates located in the sources directory, to the compiled template classes directory.- Parameters:
compilePath- additional compile path arguments for the Java compiler.
-
setNullSafeTemplateCode
public void setNullSafeTemplateCode(boolean value)
Experimental mode, that ignores anyNullPointerExceptionthat occurs in template files.- Parameters:
value- true, to enable
-
-