- 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 namefoo, the following mechanisms are supported for resolving its value (and are sought in this order):- If the variable has the special name
thisthe context object itself will be returned. This is useful when iterating over lists. - If the object is a
Map,Map.get(java.lang.Object)will be called with the stringfooas the key. - A method named
fooin the supplied object (with non-void return value). - A method named
getFooin the supplied object (with non-void return value). - A field named
fooin 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.
Booleanis used for showing or hiding sections without binding a sub-context. Arrays,IteratorandIterableimplementations 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. - If the variable has the special name
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classTemplate.FragmentEncapsulates a fragment of a template that is passed to a lambda.
-
Field Summary
Fields Modifier and Type Field Description static ObjectNO_FETCHER_FOUNDA sentinel object that can be returned by aMustache.Collectorto indicate that a variable does not exist in a particular context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringexecute(Object context)Executes this template with the given context, returning the results as a string.voidexecute(Object context, Writer out)Executes this template with the given context, writing the results to the supplied writer.voidexecute(Object context, Object parentContext, Writer out)Executes this template with the supplied context and parent context, writing the results to the supplied writer.voidvisit(Mustache.Visitor visitor)Visits the tags in this template (viavisitor) without executing it.
-
-
-
Field Detail
-
NO_FETCHER_FOUND
public static final Object NO_FETCHER_FOUND
A sentinel object that can be returned by aMustache.Collectorto indicate that a variable does not exist in a particular context.
-
-
Method Detail
-
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 (viavisitor) without executing it.- Parameters:
visitor- the visitor to be called back on each tag in the template.
-
-