Class Template

java.lang.Object
com.samskivert.mustache.Template

public class Template extends Object
Represents a compiled template. Templates are executed with a context to generate output. The context can be any tree of objects. Variables are resolved against the context. Given a name foo, the following mechanisms are supported for resolving its value (and are sought in this order):
  • If the variable has the special name this the context object itself will be returned. This is useful when iterating over lists.
  • If the object is a Map, Map.get(Object) will be called with the string foo as the key.
  • A method named foo in the supplied object (with non-void return value).
  • A method named getFoo in the supplied object (with non-void return value).
  • A field named foo in the supplied object.

The field type, method return type, or map value type should correspond to the desired behavior if the resolved name corresponds to a section. Boolean is used for showing or hiding sections without binding a sub-context. Arrays, Iterator and Iterable implementations are used for sections that repeat, with the context bound to the elements of the array, iterator or iterable. Lambdas are current unsupported, though they would be easy enough to add if desire exists. See the Mustache documentation for more details on section behavior.

  • Field Details

  • Constructor Details

  • Method Details

    • execute

      public String execute(Object context) throws MustacheException
      Executes this template with the given context, returning the results as a string.
      Throws:
      MustacheException - if an error occurs while executing or writing the template.
    • execute

      public void execute(Object context, Writer out) throws MustacheException
      Executes this template with the given context, writing the results to the supplied writer.
      Throws:
      MustacheException - if an error occurs while executing or writing the template.
    • execute

      public void execute(Object context, Object parentContext, Writer out) throws MustacheException
      Executes this template with the supplied context and parent context, writing the results to the supplied writer. The parent context will be searched for variables that cannot be found in the main context, in the same way the main context becomes a parent context when entering a block.
      Throws:
      MustacheException - if an error occurs while executing or writing the template.
    • visit

      public void visit(Mustache.Visitor visitor)
      Visits the tags in this template (via visitor) without executing it.
      Parameters:
      visitor - the visitor to be called back on each tag in the template.
    • indent

      protected Template indent(String indent)
    • replaceBlocks

      protected Template replaceBlocks(Map<String, Mustache.BlockSegment> blocks)
    • executeSegs

      protected void executeSegs(Template.Context ctx, Writer out) throws MustacheException
      Throws:
      MustacheException
    • createFragment

      protected Template.Fragment createFragment(Template.Segment[] segs, Template.Context currentCtx)
    • getValue

      protected Object getValue(Template.Context ctx, String name, int line, boolean missingIsNull)
      Called by executing segments to obtain the value of the specified variable in the supplied context.
      Parameters:
      ctx - the context in which to look up the variable.
      name - the name of the variable to be resolved.
      missingIsNull - whether to fail if a variable cannot be resolved, or to return null in that case.
      Returns:
      the value associated with the supplied name or null if no value could be resolved.
    • getCompoundValue

      protected Object getCompoundValue(Template.Context ctx, String name, int line, boolean missingIsNull)
      Decomposes the compound key name into components and resolves the value they reference.
    • getSectionValue

      protected Object getSectionValue(Template.Context ctx, String name, int line)
      Returns the value of the specified variable, noting that it is intended to be used as the contents for a section.
    • getValueOrDefault

      protected Object getValueOrDefault(Template.Context ctx, String name, int line)
      Returns the value for the specified variable, or the configured default value if the variable resolves to null. See getValue(Template.Context, String, int, boolean).
    • getValueIn

      protected Object getValueIn(Object data, String name, int line)
    • checkForMissing

      protected Object checkForMissing(String name, int line, boolean missingIsNull, Object value)
    • isThisName

      protected static boolean isThisName(String name)