- All Known Implementing Classes:
AbstractJStachio
,SpringJStachio
Render models by using reflection to lookup generated templates as well as apply
filtering and fallback mechanisms.
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.- See Also:
- API Note
- The static
render
methods are convenience methods that will use the ServiceLoader based JStachio which loads all extensions via theServiceLoader
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JStachio
defaults()
Gets default singleton ServiceLoader based jstachio.Finds a template by using the models class if possible and then applies filtering and then finally render the model to a String.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.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
.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
setStaticJStachio
(Supplier<JStachio> jstachioProvider) Set the static singleton of JStachio.boolean
supportsType
(Class<?> modelType) Determines if this jstachio can render the model type (the class annotated by JStache).
-
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.
-
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
setStaticJStachio(Supplier)
- Throws:
NullPointerException
- if jstachio is not found- See Also:
-
defaults
Gets default singleton ServiceLoader based jstachio.- Returns:
- service loaded jstachio
-
setStaticJStachio
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.
-