java.lang.Object
org.klojang.templates.Template
The
Template
class is responsible for loading and parsing templates. It
also functions as a factory for RenderSession
objects. Template
instances are unmodifiable, expensive-to-create and heavy-weight objects.
Generally though you should not cache them as this is already done by
Klojang Templates. You can disable template caching by means of a
system property. See Setting.TMPL_CACHE_SIZE
. This can be useful during
development and/or debugging as the template file will be re-loaded and re-parsed
every time you press the refresh button in the browser, allowing you to edit the
template in between.- Author:
- Ayco Holleman
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The name given to the root template: "{root}". -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of templates nested inside thisTemplate
(non-recursive).int
Returns the total number of variables in thisTemplate
.boolean
Determines whether this template is equal to the specified object.static Template
Parses the specified file into aTemplate
.static Template
fromResolver
(PathResolver resolver, String path) Creates aTemplate
from the source provided by the specifiedPathResolver
.static Template
fromResource
(Class<?> clazz, String path) Parses the specified resource into aTemplate
.static Template
fromString
(Class<?> clazz, String source) Parses the specified string into aTemplate
.static Template
fromString
(String source) Parses the specified string into aTemplate
.getName()
Returns the name of thisTemplate
.getNames()
Returns the names of all variables and nested templates within thisTemplate
(non-recursive).getNestedTemplate
(String name) Returns the nested template identified by the specified name.Returns the names of all templates nested inside thisTemplate
(non-recursive).Returns all templates nested inside thisTemplate
(non-recursive).Returns the template inside which thisTemplate
is nested.Returns the root template of this (nested)Template
.Returns all occurrences of all variables within thisTemplate
.Returns the names of the variables in thisTemplate
, in order of their first appearance in the template.int
hashCode()
Returns the hash code of thisTemplate
.boolean
hasNestedTemplate
(String name) Returnstrue
if thisTemplate
contains a nested template with the specified name.boolean
hasVariable
(String name) Returnstrue
if thisTemplate
directly contains a variable with the specified name.boolean
Returnstrue
if there is at least one variable in thisTemplate
, or anyTemplate descending
from it.boolean
Returnstrue
if this is a text-only template.Returns aRenderSession
that can be used to populate and render thisTemplate
.newRenderSession
(AccessorRegistry accessors) Returns aRenderSession
that can be used to populate and render thisTemplate
.newRenderSession
(AccessorRegistry accessors, StringifierRegistry stringifiers) Returns aRenderSession
that you can use to populate and render thisTemplate
.newRenderSession
(StringifierRegistry stringifiers) Returns aRenderSession
that can be used to populate and render thisTemplate
.path()
Returns anOptional
containing the path to the source code of this template, or an emptyOptional
if the template was created from a string.toString()
More or less re-assembles to source code from the constituent parts of theTemplate
.
-
Field Details
-
ROOT_TEMPLATE_NAME
The name given to the root template: "{root}". AnyTemplate
that is explicitly instantiated by calling one of thefromXXX()
methods gets this name.- See Also:
-
-
Method Details
-
fromString
Parses the specified string into aTemplate
. If the string contains anyinclude
tags (like~%%include:/path/to/foo.html%%
), the path will be interpreted as a file system resource. Templates created from a string are never cached.- Parameters:
source
- the source code for theTemplate
- Returns:
- a new
Template
instance - Throws:
ParseException
- if the template source contains a syntax error
-
fromString
Parses the specified string into aTemplate
. The specified class will be used to include other templates usingClass.getResourceAsStream()
. Templates created from a string are never cached.- Parameters:
clazz
- aClass
object that provides access to the included template file by callinggetResourceAsStream
on itsource
- the source code for theTemplate
- Returns:
- a
Template
instance - Throws:
ParseException
- if the template source contains a syntax error
-
fromResource
Parses the specified resource into aTemplate
. Templates created from a classpath resource are always cached. Thus, calling this method multiple times with the sameclazz
andpath
arguments will always return the same instance. Make sure the provided class is publicly accessible. Otherwise Klojang Templates cannot use it to open anInputStream
to the resource.- Parameters:
clazz
- aClass
object that provides access to the template file by callinggetResourceAsStream
on itpath
- the location of the template file- Returns:
- a
Template
instance - Throws:
ParseException
- if the template source contains a syntax error
-
fromFile
Parses the specified file into aTemplate
. Templates created from file are always cached. Thus, calling this method multiple times with the samepath
argument will always return the same instance.- Parameters:
path
- the path of the file to be parsed- Returns:
- a
Template
instance - Throws:
ParseException
- if the template source contains a syntax error
-
fromResolver
Creates aTemplate
from the source provided by the specifiedPathResolver
. Templates created using aPathResolver
are always cached. Thus, calling this method multiple times with the samePathResolver
(as per itsequals()
method) and the same path will always return the same instance.- Parameters:
resolver
- thePathResolver
path
- the path to be resolved by thePathResolver
- Returns:
- a
Template
instance - Throws:
ParseException
- if the template source contains a syntax error
-
getName
Returns the name of thisTemplate
. It thisTemplate
was explicitly instantiated using one of thefromXXX()
methods, its name will be "{root}"; otherwise it is a nested template and its name will be extracted from the source code (e.g.~%%begin:foo%
).- Returns:
- the name of this
Template
-
getParent
Returns the template inside which thisTemplate
is nested. If thisTemplate
is the root template (the template that was explicitly created by a call to one of thefromXXX()
methods), this method returnsnull
.- Returns:
- the template inside which this
Template
is nested
-
getRootTemplate
Returns the root template of this (nested)Template
. If thisTemplate
is the root template, then this method returnsthis
. Only (and all) templates that were created using one of thefromXXX()
methods are root templates.- Returns:
- the root template of this
Template
-
path
Returns an
Optional
containing the path to the source code of this template, or an emptyOptional
if the template was created from a string. In other words, forincluded
templates this method (by definition) returns a non-emptyOptional
. For inline templates this method (by definition) returns an emptyOptional
. For templates that were explicitly created using one of thefromXXX()
methods, the return value depends on whether it wasfromString()
or one of the otherfromXXX()
methods.- Returns:
- the path to the source code for this
Template
-
getVariables
Returns the names of the variables in thisTemplate
, in order of their first appearance in the template. The returned set only contains the names of variables that reside directly inside thisTemplate
. Variables inside nested templates are ignored. The returnedSet
is unmodifiable.- Returns:
- the names of all variables in this
Template
- See Also:
-
getVariableOccurrences
Returns all occurrences of all variables within thisTemplate
. Note that a variable name may occur multiple times within the same template.- Returns:
- all occurrences of all variables within this
Template
-
countVariableOccurrences
public int countVariableOccurrences()Returns the total number of variables in thisTemplate
. Note that a variable name may occur multiple times within the same template. This method does not count the number of unique variable names. To get that number, callgetVariables().size()
.- Returns:
- the total number of variables in this
Template
-
hasVariable
Returnstrue
if thisTemplate
directly contains a variable with the specified name. Variables inside nested templates are not considered.- Parameters:
name
- the name of the variable- Returns:
true
if thisTemplate
contains a variable with the specified name
-
getNestedTemplates
Returns all templates nested inside thisTemplate
(non-recursive). The returnedList
is unmodifiable.- Returns:
- all templates nested inside this
Template
-
getNestedTemplateNames
Returns the names of all templates nested inside thisTemplate
(non-recursive). The returnedSet
is unmodifiable.- Returns:
- the names of all nested templates
-
hasNestedTemplate
Returnstrue
if thisTemplate
contains a nested template with the specified name.- Parameters:
name
- the name of the nested template- Returns:
true
if thisTemplate
contains a nested template with the specified name
-
countNestedTemplates
public int countNestedTemplates()Returns the number of templates nested inside thisTemplate
(non-recursive).- Returns:
- the number of nested templates
-
getNestedTemplate
Returns the nested template identified by the specified name. This method throws anIllegalArgumentException
if no nested template has the specified name.- Parameters:
name
- the name of a nested template- Returns:
- the
Template
with the specified name
-
getNames
Returns the names of all variables and nested templates within thisTemplate
(non-recursive). The returnedList
is unmodifiable.- Returns:
- the names of all variables and nested templates in this
Template
-
isTextOnly
public boolean isTextOnly()Returnstrue
if this is a text-only template. In other words, if this is a template without any variables or nested templates.- Returns:
- whether this is a text-only template
-
hasVariables
public boolean hasVariables()Returnstrue
if there is at least one variable in thisTemplate
, or anyTemplate descending
from it. Contrast this withhasVariable(String)
, which only checks for variables directly contained in thisTemplate
.- Returns:
true
if there are no variables in thisTemplate
, or anyTemplate descending
from it
-
newRenderSession
Returns aRenderSession
that can be used to populate and render thisTemplate
. TheRenderSession
uses thepredefined accessors
to extract values from source data objects, and thepredefined stringifiers
to stringify those values.- Returns:
- a
RenderSession
- See Also:
-
newRenderSession
Returns aRenderSession
that can be used to populate and render thisTemplate
. TheRenderSession
will use thepredefined accessors
to extract values from source data objects, and the specifiedStringifierRegistry
to stringify those values.- Parameters:
stringifiers
- theStringifierRegistry
used to supply theRenderSession
withstringifiers
- Returns:
- a new
RenderSession
-
newRenderSession
Returns aRenderSession
that can be used to populate and render thisTemplate
. TheRenderSession
will use the specifiedAccessorRegistry
to extract values from source data, and thepredefined stringifiers
to stringify those values.- Parameters:
accessors
- theAccessorRegistry
used to supply theRenderSession
withaccessors
- Returns:
- a new
RenderSession
-
newRenderSession
Returns aRenderSession
that you can use to populate and render thisTemplate
. TheRenderSession
will use the specifiedAccessorRegistry
to extract values from source data, and the specifiedStringifierRegistry
to stringify those values.- Parameters:
accessors
- theAccessorRegistry
used to supply theRenderSession
withaccessors
stringifiers
- theStringifierRegistry
used to supply theRenderSession
withstringifiers
- Returns:
- a new
RenderSession
-
equals
Determines whether this template is equal to the specified object. Two templates are equals if they were created from the same path andPathResolver
.Template
instances that were created from a string are never equal to any otherTemplate
instance, even if the other instance was created from exactly the sameString
. -
hashCode
public int hashCode()Returns the hash code of thisTemplate
. -
toString
More or less re-assembles to source code from the constituent parts of theTemplate
. Note, however, that ditch blocks are ditched early on in the parsing process and there is no trace of them left in the resultingTemplate
instance.
-