Interface JStachio

All Superinterfaces:
Renderer<Object>
All Known Implementing Classes:
AbstractJStachio, SpringJStachio

public interface JStachio extends Renderer<Object>
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 the ServiceLoader.
  • Method Details

    • execute

      void execute(Object model, Appendable appendable) throws IOException
      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 interface Renderer<Object>
      Parameters:
      model - a model assumed never to be null.
      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 the StringBuilder.

      A convenience method that does not throw IOException when using StringBuilder.

      Specified by:
      execute in interface Renderer<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.

      Specified by:
      execute in interface Renderer<Object>
      Parameters:
      model - never null.
      Returns:
      the rendered model.
    • supportsType

      boolean supportsType(Class<?> modelType)
      Determines if this jstachio can render the model type (the class annotated by JStache).
      Parameters:
      modelType - the models class (the one annotated with JStache and not the Templates class)
      Returns:
      true if this jstachio can render instances of modelType
    • render

      static void render(Object model, Appendable a) throws IOException
      Executes the ServiceLoader instance of JStachio execute(Object, Appendable).
      Parameters:
      model - never null
      a - appendable never null
      Throws:
      IOException - if there is an error using the appendable
      See Also:
    • render

      Executes the ServiceLoader instance of JStachio execute(Object, StringBuilder).
      Parameters:
      model - never null
      a - appendable never null
      Returns:
      the passed in StringBuilder
      See Also:
    • render

      static String render(Object model)
      Executes the ServiceLoader instance of JStachio execute(Object).
      Parameters:
      model - the root context model. Never null.
      Returns:
      the rendered string.
      See Also:
    • of

      static JStachio of()
      Gets the static singleton jstachio.
      Returns:
      the jstachio from setStaticJStachio(Supplier)
      Throws:
      NullPointerException - if jstachio is not found
      See Also:
    • defaults

      static JStachio defaults()
      Gets default singleton ServiceLoader based jstachio.
      Returns:
      service loaded jstachio
    • setStaticJStachio

      static void setStaticJStachio(Supplier<JStachio> jstachioProvider)
      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.