Interface TemplateModel


public sealed interface TemplateModel
A template and model combined with convenience methods.

This tuple like object is useful way to combine the correct template with a model if you have programmatic access to the template. This can be useful to a web framework perhaps as the return type of a web controller and because the template is already located the web framework does not have to re-find the template. An analog in the Spring framework would be ModelAndView.

It is purposely not parameterized for ease of use as dealing with generics via reflection can be difficult.

Author:
agentgt
API Note
the interface is purposely sealed for now to see usage and feedback and one can make their own similar version with JStacheInterfaces.
  • Method Summary

    Modifier and Type
    Method
    Description
    default String
    Renders the template to a String.
    <A extends Output<E>, E extends Exception>
    A
    execute(A output)
    Renders the passed in model directly to an appendable like output.
    Model.
    static <T> TemplateModel
    of(Template.EncodedTemplate<T> template, T model)
    Creates a template model pair.
    static <T> TemplateModel
    of(Template<T> template, T model)
    Creates a template model pair.
    Template.
    default <A extends Output.EncodedOutput<E>, E extends Exception>
    A
    write(A output)
    Renders the passed in model directly to a binary stream possibly leveraging pre-encoded parts of the template.
    default void
    write(OutputStream outputStream)
    Convenience method to write directly to an outputstream with the templates character encoding.
  • Method Details

    • template

      Template.
      Returns:
      template to use for execution
    • model

      Model.
      Returns:
      model to be executed on
    • execute

      <A extends Output<E>, E extends Exception> A execute(A output) throws E
      Renders the passed in model directly to an appendable like output.
      Type Parameters:
      A - output type
      E - error type
      Parameters:
      output - to write to.
      Returns:
      the passed in output
      Throws:
      E - if an error occurs while writing to output
    • write

      default <A extends Output.EncodedOutput<E>, E extends Exception> A write(A output) throws E
      Renders the passed in model directly to a binary stream possibly leveraging pre-encoded parts of the template.
      Type Parameters:
      A - output type
      E - error type
      Parameters:
      output - to write to.
      Returns:
      the passed in output
      Throws:
      E - if an error occurs while writing to output
    • write

      default void write(OutputStream outputStream) throws IOException
      Convenience method to write directly to an outputstream with the templates character encoding.
      Parameters:
      outputStream - outputStream to write to
      Throws:
      IOException - if an error happens while writting to the stream.
    • execute

      default String execute()
      Renders the template to a String.
      Returns:
      the executed template as a string.
    • of

      static <T> TemplateModel of(Template.EncodedTemplate<T> template, T model)
      Creates a template model pair.
      Type Parameters:
      T - model type
      Parameters:
      template - encoded template
      model - model instance
      Returns:
      the template executable designed for encoded templates.
    • of

      static <T> TemplateModel of(Template<T> template, T model)
      Creates a template model pair.
      Type Parameters:
      T - model type
      Parameters:
      template - encoded template
      model - model instance
      Returns:
      the template executable.