- 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
JStachioExtensions. You can customize it by adding jars that have provided
JStachioExtensions 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
rendermethods are convenience methods that will by default use the ServiceLoader based JStachio which loads all extensions via theServiceLoader.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JStachiodefaults()Gets default singleton ServiceLoader based jstachio.default StringFinds a template by using the models class if possible and then applies filtering and then finally render the model to a String.default voidexecute(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 StringBuilderexecute(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 JStachioof()Gets the static singleton jstachio.static StringExecutes the ServiceLoader instance of JStachioexecute(Object).static voidrender(Object model, Appendable a) Executes the ServiceLoader instance of JStachioexecute(Object, Appendable).static StringBuilderrender(Object model, StringBuilder a) Executes the ServiceLoader instance of JStachioexecute(Object, StringBuilder).static voidSet the static singleton of JStachio.booleansupportsType(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:
executein 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
IOExceptionwhen using StringBuilder.- Specified by:
executein 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
TemplateModelshould 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 aTemplateModelcontaining 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
TemplateModelpassed in.
-
supportsType
Determines if this jstachio can render the model type (the class annotated by JStache).- Parameters:
modelType- the models class (the one annotated withJStacheand 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- nevernulla- 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- nevernulla- 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.
-