Package gg.jte
Class TemplateEngine
- java.lang.Object
-
- gg.jte.TemplateEngine
-
public final class TemplateEngine extends java.lang.Objectjte 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, ContentType contentType)Creates a new template engine.static TemplateEnginecreate(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType)Creates a new template engine.static TemplateEnginecreate(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader)Creates a new template engine.static TemplateEnginecreate(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader, java.lang.String packageName)Creates a new template engine.static TemplateEnginecreatePrecompiled(ContentType contentType)Creates a new template engine.static TemplateEnginecreatePrecompiled(java.nio.file.Path classDirectory, ContentType contentType)Creates a new template engine.static TemplateEnginecreatePrecompiled(java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader)Creates a new template engine.static TemplateEnginecreatePrecompiled(java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader, java.lang.String packageName)Creates a new template engine.java.util.List<java.lang.String>generateAll()Generates all template classes in the sources directory, to the compiled template classes directory.java.util.Map<java.lang.String,java.lang.Class<?>>getParamInfo(java.lang.String name)Obtain parameter information about a specific template.java.util.List<java.lang.String>getTemplatesUsing(java.lang.String name)booleanhasTemplate(java.lang.String name)java.util.List<java.lang.String>precompileAll()Compiles all templates located in the sources directory, to the compiled template classes directory.java.util.List<java.lang.String>precompileAll(java.util.List<java.lang.String> classPath)Compiles all templates located in the sources directory, to the compiled template classes directory.voidprepareForRendering(java.lang.String name)Prepares the template with the given name for renderingTemplateEnginereloadPrecompiled(TemplateEngine precompiler)Useful, if this engine is in precompiled mode (probably production) but you still want to be able to apply a hotfix without deployment.TemplateEnginereloadPrecompiled(java.nio.file.Path classDirectory)Useful, if this engine is in precompiled mode (probably production) but you still want to be able to apply a hotfix without deployment.voidrender(java.lang.String name, java.lang.Object param, TemplateOutput output)Renders the template with the given name.voidrender(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output)Renders the template with the given name.voidrenderLayout(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output)Renders a layout with the given name.voidrenderTag(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output)Renders a tag with the given name.voidsetBinaryStaticContent(boolean binaryStaticContent)Experimental setting, that UTF-8 encodes all static template parts.voidsetClassPath(java.util.List<java.lang.String> classPath)The class path used for compiling templates.voidsetCompileArgs(java.lang.String... compileArgs)Sets additional compiler arguments for jte templates.voidsetGenerateNativeImageResources(boolean value)Whether to try and generate configuration files to support Graal native-image.voidsetHtmlAttributes(java.lang.String... htmlAttributes)Intercepts the given html attributes for configured htmlTags during template compilation and calls the configured htmlInterceptor during template rendering.voidsetHtmlCommentsPreserved(boolean htmlCommentsPreserved)By default, jte omits all HMTL/CSS/JS comments, when compiling withContentType.Html.voidsetHtmlInterceptor(HtmlInterceptor htmlInterceptor)Interceptor that is called during template rendering when one of the configured htmlTags is rendered.voidsetHtmlPolicy(HtmlPolicy htmlPolicy)Policy that checks the parsed HTML at compile time.voidsetHtmlTags(java.lang.String... htmlTags)Intercepts the given html tags during template compilation and calls the configured htmlInterceptor during template rendering.voidsetProjectNamespace(java.lang.String projectNamespace)"group/artifact" of the project using jte.voidsetTargetResourceDirectory(java.nio.file.Path targetResourceDirectory)Directory in which to generate non-java files (resources).voidsetTrimControlStructures(boolean value)Experimental mode, that trims control structures, resulting in prettier output.
-
-
-
Method Detail
-
create
public static TemplateEngine create(CodeResolver codeResolver, ContentType contentType)
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 templatescontentType- the content type of all templates this engine manages- Returns:
- a fresh TemplateEngine instance
-
create
public static TemplateEngine create(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType)
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 tocontentType- the content type of all templates this engine manages- Returns:
- a fresh TemplateEngine instance
-
create
public static TemplateEngine create(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader)
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 tocontentType- the content type of all templates this engine managesparentClassLoader- the parent classloader to use, or null to use the application class loader as parent- Returns:
- a fresh TemplateEngine instance
-
create
public static TemplateEngine create(CodeResolver codeResolver, java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader, java.lang.String packageName)
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 tocontentType- the content type of all templates this engine managesparentClassLoader- the parent classloader to use, or null to use the application class loader as parentpackageName- the package name, where template classes are generated to- Returns:
- a fresh TemplateEngine instance
-
createPrecompiled
public static TemplateEngine createPrecompiled(java.nio.file.Path classDirectory, ContentType contentType)
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 locatedcontentType- the content type of all templates this engine manages- Returns:
- a fresh TemplateEngine instance
-
createPrecompiled
public static TemplateEngine createPrecompiled(ContentType contentType)
Creates a new template engine. All templates must have been precompiled to Java class files already. The template engine will load them via the application class loader. This means all template classes must be bundled in you application JAR file. No JDK is required. This is recommended when running templates in production. How to precompile templates: https://github.com/casid/jte/blob/master/DOCUMENTATION.md#precompiling-templates- Parameters:
contentType- the content type of all templates this engine manages- Returns:
- a fresh TemplateEngine instance
-
createPrecompiled
public static TemplateEngine createPrecompiled(java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader)
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 locatedcontentType- the content type of all templates this engine managesparentClassLoader- the parent classloader to use, or null to use the application class loader as parent (only has an effect if classDirectory is not null)- Returns:
- a fresh TemplateEngine instance
-
createPrecompiled
public static TemplateEngine createPrecompiled(java.nio.file.Path classDirectory, ContentType contentType, java.lang.ClassLoader parentClassLoader, java.lang.String packageName)
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 locatedcontentType- the content type of all templates this engine managesparentClassLoader- the parent classloader to use, or null to use the application class loader as parent (only has an effect if classDirectory is not null)packageName- the package name, where template classes are generated to- Returns:
- a fresh TemplateEngine instance
-
render
public void render(java.lang.String name, java.lang.Object param, TemplateOutput output) throws TemplateExceptionRenders the template with the given name. It is preferred to use this method, if all your templates have exactly one parameter.- Parameters:
name- the template name relative to the specified root directory, for instance "pages/welcome.jte".param- the param 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.
-
render
public void render(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output) throws TemplateExceptionRenders the template with the given name. Parameters in the params map are mapped to the corresponding parameters in the template. Template parameters with a default value don't have to be provided in the map.- Parameters:
name- the template name relative to the specified root directory, for instance "pages/welcome.jte".params- the parameters passed to the template as key value pairs.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.
-
renderTag
public void renderTag(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output) throws TemplateExceptionRenders a tag with the given name. This comes at the cost of an extra method invocation and losing the type safety for params that jte usually provides. However, this is useful while migrating to jte. For instance, you can port a JSP tag to a jte tag and invoke the new jte tag from all other JSPs, so that there are no redundant implementations during the migration.- Parameters:
name- the template name relative to the specified root directory, for instance "tag/myTag.jte".params- map of parameters that should be passed to the tag.output- any implementation ofTemplateOutput, where the template will be written to.- Throws:
TemplateException- in case the tag failed to render, containing information where the error happened.
-
renderLayout
public void renderLayout(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, TemplateOutput output) throws TemplateExceptionRenders a layout with the given name. This comes at the cost of an extra method invocation and losing the type safety for params that jte usually provides. However, this is useful while migrating to jte. For instance, you can port a JSP layout to a jte layout and invoke the new jte layout from all other JSPs, so that there are no redundant implementations during the migration.- Parameters:
name- the template name relative to the specified root directory, for instance "layout/myLayout.jte".params- map of parameters that should be passed to the layout.output- any implementation ofTemplateOutput, where the template will be written to.- Throws:
TemplateException- in case the layout failed to render, containing information where the error happened.
-
hasTemplate
public boolean hasTemplate(java.lang.String name)
-
getTemplatesUsing
public java.util.List<java.lang.String> getTemplatesUsing(java.lang.String name)
-
getParamInfo
public java.util.Map<java.lang.String,java.lang.Class<?>> getParamInfo(java.lang.String name) throws TemplateExceptionObtain parameter information about a specific template.- Parameters:
name- the template name relative to the specified root directory, for instance "tag/example.jte".- Returns:
- a map containing all template parameters names and their classes
- Throws:
TemplateException- in case parameter information is not available (jte classes must be compiled with -parameters compiler flag.)
-
prepareForRendering
public void prepareForRendering(java.lang.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.
-
generateAll
public java.util.List<java.lang.String> generateAll()
Generates all template classes in the sources directory, to the compiled template classes directory. This only generates .java files, not .class files.- Returns:
- list of .java template files that were generated
-
precompileAll
public java.util.List<java.lang.String> precompileAll()
Compiles all templates located in the sources directory, to the compiled template classes directory.- Returns:
- list of .java template files that were compiled
-
precompileAll
public java.util.List<java.lang.String> precompileAll(java.util.List<java.lang.String> classPath)
Compiles all templates located in the sources directory, to the compiled template classes directory.- Parameters:
classPath- additional class path arguments for the Java compiler. See alsosetClassPath(List)- Returns:
- list of .java template files that were compiled
-
reloadPrecompiled
public TemplateEngine reloadPrecompiled(TemplateEngine precompiler) throws TemplateException
Useful, if this engine is in precompiled mode (probably production) but you still want to be able to apply a hotfix without deployment. This template engine will be entirely unaffected by this call. Instead, a fresh template engine will be created. When this call succeeds, you can safely switch the template engine reference to the new instance. The old instance including all old templates should then be subject to garbage collection. This only works if you're running on the JDK and if templates have their own classloader.- Parameters:
precompiler- a template engine that is configured exactly as you usually would precompile your templates.- Returns:
- a fresh template engine with a warmed up cache.
- Throws:
TemplateException- in case there was a compilation error, in this case you should keep the current engine running!
-
reloadPrecompiled
public TemplateEngine reloadPrecompiled(java.nio.file.Path classDirectory) throws TemplateException
Useful, if this engine is in precompiled mode (probably production) but you still want to be able to apply a hotfix without deployment. This template engine will be entirely unaffected by this call. Instead, a fresh template engine will be created. When this call succeeds, you can safely switch the template engine reference to the new instance. The old instance including all old templates should then be subject to garbage collection. Basically you could recompile all templates on your build server, upload them to your production server and call this method afterwards. This only works if templates have their own classloader.- Parameters:
classDirectory- the class directory to load the new templates from.- Returns:
- a fresh template engine with a warmed up cache.
- Throws:
TemplateException- in case there was an error during class loading, in this case you should keep the current engine running!
-
setCompileArgs
public void setCompileArgs(java.lang.String... compileArgs)
Sets additional compiler arguments for jte templates.- Parameters:
compileArgs- for instance templateEngine.setCompileArgs("--enable-preview", "--release", "" + Runtime.version().feature());
-
setTrimControlStructures
public void setTrimControlStructures(boolean value)
Experimental mode, that trims control structures, resulting in prettier output.- Parameters:
value- true, to enable
-
setHtmlPolicy
public void setHtmlPolicy(HtmlPolicy htmlPolicy)
Policy that checks the parsed HTML at compile time.- Parameters:
htmlPolicy- the policy- Throws:
java.lang.NullPointerException- if policy is null
-
setHtmlTags
public void setHtmlTags(java.lang.String... htmlTags)
Intercepts the given html tags during template compilation and calls the configured htmlInterceptor during template rendering.- Parameters:
htmlTags- tags to be intercepted, for instance setHtmlTags("form", "input");
-
setHtmlAttributes
public void setHtmlAttributes(java.lang.String... htmlAttributes)
Intercepts the given html attributes for configured htmlTags during template compilation and calls the configured htmlInterceptor during template rendering.- Parameters:
htmlAttributes- attributes to be intercepted, for instance setHtmlAttributes("class");
-
setHtmlInterceptor
public void setHtmlInterceptor(HtmlInterceptor htmlInterceptor)
Interceptor that is called during template rendering when one of the configured htmlTags is rendered. This allows to integrate existing frameworks into jte.- Parameters:
htmlInterceptor- the interceptor
-
setHtmlCommentsPreserved
public void setHtmlCommentsPreserved(boolean htmlCommentsPreserved)
By default, jte omits all HMTL/CSS/JS comments, when compiling withContentType.Html. If you don't want this behavior, you can disable it here.- Parameters:
htmlCommentsPreserved- true, to preserve HTML comments in templates
-
setBinaryStaticContent
public void setBinaryStaticContent(boolean binaryStaticContent)
Experimental setting, that UTF-8 encodes all static template parts.- Parameters:
binaryStaticContent- true, to pre-generate UTF-8 encoded byte arrays for all static template parts
-
setClassPath
public void setClassPath(java.util.List<java.lang.String> classPath)
The class path used for compiling templates.- Parameters:
classPath- list of elements on the class path
-
setTargetResourceDirectory
public void setTargetResourceDirectory(java.nio.file.Path targetResourceDirectory)
Directory in which to generate non-java files (resources). Typically set by plugin rather than end user. Optional - if null, resources will not be generated- Parameters:
targetResourceDirectory- directory to generate resources in
-
setProjectNamespace
public void setProjectNamespace(java.lang.String projectNamespace)
"group/artifact" of the project using jte. Typically set by plugin rather than end user. If null, the compiler will make one up.- Parameters:
projectNamespace- "groupId/artifactId"
-
setGenerateNativeImageResources
public void setGenerateNativeImageResources(boolean value)
Whether to try and generate configuration files to support Graal native-image. If true, setTargetResourceDirectory should also be used to indicate where to put the resources.- Parameters:
value- true to generate native image configuration resources
-
-