- All Known Subinterfaces:
ContextJStachio
- All Known Implementing Classes:
AbstractJStachio
,SpringJStachio
Example Usage
@JStache(template = "Hello {{name}}!")
public record HelloWorld(String name) {}
public static String output(String name) {
//Normally you would have to use generated class HelloWorldRenderer
//but this JStachio allows you to render directly
//from the model.
return JStachio.render(new HelloWorld(name));
}
Not only is the above more convenient than using the raw generated code
it also allows additional custom runtime behavior like filtering as well as allows
easier integration with web frameworks.
Customize
The defaultJStachio
uses the ServiceLoader
to load
JStachioExtension
s. You can customize it by adding jars that have provided
JStachioExtension
s or by adjusting config.
If you would like to create your own JStachio
instead of the default you can
either extend AbstractJStachio
or use JStachioFactory.builder()
. If you
want your custom JStachio
to be set as the default such that the static render
methods on this class call it you can do that with setStatic(Supplier)
.
While this interface is not sealed it is strongly recommended that you do not
implement this interface! It has been left unsealed for mocking and testing
purposes.
- See Also:
- API Note
- The static
render
methods are convenience methods that will by default use the ServiceLoader based JStachio which loads all extensions via theServiceLoader
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JStachio
defaults()
Gets default singleton ServiceLoader based jstachio.default String
Finds a template by using the models class if possible and then applies filtering and then finally render the model to a String.default void
execute
(Object model, Appendable appendable) Finds a template by using the models class if possible and then applies filtering and then finally render the model by writing to the appendable.default StringBuilder
execute
(Object model, StringBuilder sb) Finds a template by using the models class if possible and then applies filtering and then finally render the model by writing to theStringBuilder
.findTemplate
(Object model) Finds a template by model.static JStachio
of()
Gets the static singleton jstachio.static String
Executes the ServiceLoader instance of JStachioexecute(Object)
.static void
render
(Object model, Appendable a) Executes the ServiceLoader instance of JStachioexecute(Object, Appendable)
.static StringBuilder
render
(Object model, StringBuilder a) Executes the ServiceLoader instance of JStachioexecute(Object, StringBuilder)
.static void
Set the static singleton of JStachio.boolean
supportsType
(Class<?> modelType) Determines if this jstachio can render the model type (the class annotated by JStache).<A extends Output.EncodedOutput<E>,
E extends Exception>
ARenders the passed in model directly to a binary stream possibly leveraging pre-encoded parts of the template.
-
Method Details
-
execute
Finds a template by using the models class if possible and then applies filtering and then finally render the model by writing to the appendable.Renders the passed in model.
- Specified by:
execute
in interfaceRenderer<Object>
- Parameters:
model
- a model assumed never to benull
.appendable
- the appendable to write to.- Throws:
IOException
- if there is an error writing to the appendable
-
execute
Finds a template by using the models class if possible and then applies filtering and then finally render the model by writing to theStringBuilder
.A convenience method that does not throw
IOException
when using StringBuilder.- Specified by:
execute
in interfaceRenderer<Object>
- Parameters:
model
- a model assumed never to be null.sb
- should never be null.- Returns:
- the passed in
StringBuilder
.
-
execute
Finds a template by using the models class if possible and then applies filtering and then finally render the model to a String.Convenience method that directly renders the model as a String.
-
write
Renders the passed in model directly to a binary stream possibly leveraging pre-encoded parts of the template. This may improve performance when rendering UTF-8 to an OutputStream as some of the encoding is done in advance. Because the encoding is done statically you cannot pass the charset in. The chosen charset comes fromJStacheConfig.charset()
.- Type Parameters:
A
- output typeE
- error type- Parameters:
model
- a model assumed never to benull
.output
- to write to.- Returns:
- the passed in output for convenience
- Throws:
UnsupportedCharsetException
- if the encoding of the output does not match the template.E
- if an error occurs while writing to output
-
findTemplate
Finds a template by model. This is useful if you need metadata before writing such as charset and media type for HTTP output which the template has.The returned template is decorated if filtering is on and a filter that is not the template is applied.
Passing in a
TemplateModel
should work as well and the returned template will be able to execute the TemplateModel as though it were a regular model.- Parameters:
model
- the actual model or aTemplateModel
containing the model- Returns:
- a filtered template
- Throws:
NoSuchElementException
- if a template is not found and no other lookup errors happen.Exception
- if template cannot be found for unexpected reasons such as reflection errors.- API Note
- implementations should handle
TemplateModel
passed in.
-
supportsType
Determines if this jstachio can render the model type (the class annotated by JStache).- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)- Returns:
- true if this jstachio can render instances of modelType
-
render
Executes the ServiceLoader instance of JStachioexecute(Object, Appendable)
.- Parameters:
model
- nevernull
a
- appendable nevernull
- Throws:
IOException
- if there is an error using the appendable- See Also:
-
render
Executes the ServiceLoader instance of JStachioexecute(Object, StringBuilder)
.- Parameters:
model
- nevernull
a
- appendable nevernull
- Returns:
- the passed in
StringBuilder
- See Also:
-
render
Executes the ServiceLoader instance of JStachioexecute(Object)
.- Parameters:
model
- the root context model. Nevernull
.- Returns:
- the rendered string.
- See Also:
-
of
Gets the static singleton jstachio.- Returns:
- the jstachio from
setStatic(Supplier)
- Throws:
NullPointerException
- if jstachio is not found- See Also:
-
defaults
Gets default singleton ServiceLoader based jstachio.- Returns:
- service loaded jstachio
-
setStatic
Set the static singleton of JStachio.Useful if you would like to avoid using the default ServiceLoader mechanism.
- Parameters:
jstachioProvider
- if null a NPE will be thrown.- API Note
- the provider will be called on every call of
of()
and thus to avoid constant recreation it is recommend the supplier be memoized/cached.
-