- java.lang.Object
-
- com.samskivert.mustache.Template
-
public class Template extends java.lang.ObjectRepresents 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 protected static classTemplate.ContextclassTemplate.FragmentEncapsulates a fragment of a template that is passed to a lambda.protected static classTemplate.KeyUsed to cache variable fetchers for a given context class, name combination.protected static classTemplate.SegmentA template is broken into segments.
-
Field Summary
Fields Modifier and Type Field Description protected Mustache.Compiler_compilerprotected java.util.Map<Template.Key,Mustache.VariableFetcher>_fcacheprotected Template.Segment[]_segsprotected static java.lang.StringDOT_NAMEprotected static java.lang.StringFIRST_NAMEprotected static java.lang.StringINDEX_NAMEprotected static java.lang.StringLAST_NAMEstatic java.lang.ObjectNO_FETCHER_FOUNDA sentinel object that can be returned by aMustache.Collectorto indicate that a variable does not exist in a particular context.protected static Mustache.VariableFetcherNOT_FOUND_FETCHERA fetcher cached for lookups that failed to find a fetcher.protected static java.lang.StringTHIS_NAME
-
Constructor Summary
Constructors Modifier Constructor Description protectedTemplate(Template.Segment[] segs, Mustache.Compiler compiler)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.ObjectcheckForMissing(java.lang.String name, int line, boolean missingIsNull, java.lang.Object value)protected Template.FragmentcreateFragment(Template.Segment[] segs, Template.Context currentCtx)java.lang.Stringexecute(java.lang.Object context)Executes this template with the given context, returning the results as a string.voidexecute(java.lang.Object context, java.io.Writer out)Executes this template with the given context, writing the results to the supplied writer.voidexecute(java.lang.Object context, java.lang.Object parentContext, java.io.Writer out)Executes this template with the supplied context and parent context, writing the results to the supplied writer.protected voidexecuteSegs(Template.Context ctx, java.io.Writer out)protected java.lang.ObjectgetCompoundValue(Template.Context ctx, java.lang.String name, int line, boolean missingIsNull)Decomposes the compound keynameinto components and resolves the value they reference.protected java.lang.ObjectgetSectionValue(Template.Context ctx, java.lang.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.protected java.lang.ObjectgetValue(Template.Context ctx, java.lang.String name, int line, boolean missingIsNull)Called by executing segments to obtain the value of the specified variable in the supplied context.protected java.lang.ObjectgetValueIn(java.lang.Object data, java.lang.String name, int line)protected java.lang.ObjectgetValueOrDefault(Template.Context ctx, java.lang.String name, int line)Returns the value for the specified variable, or the configured default value if the variable resolves to null.protected Templateindent(java.lang.String indent)protected static booleanisThisName(java.lang.String name)protected TemplatereplaceBlocks(java.util.Map<java.lang.String,Mustache.BlockSegment> blocks)voidvisit(Mustache.Visitor visitor)Visits the tags in this template (viavisitor) without executing it.
-
-
-
Field Detail
-
NO_FETCHER_FOUND
public static final java.lang.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.
-
_segs
protected final Template.Segment[] _segs
-
_compiler
protected final Mustache.Compiler _compiler
-
_fcache
protected final java.util.Map<Template.Key,Mustache.VariableFetcher> _fcache
-
DOT_NAME
protected static final java.lang.String DOT_NAME
- See Also:
- Constant Field Values
-
THIS_NAME
protected static final java.lang.String THIS_NAME
- See Also:
- Constant Field Values
-
FIRST_NAME
protected static final java.lang.String FIRST_NAME
- See Also:
- Constant Field Values
-
LAST_NAME
protected static final java.lang.String LAST_NAME
- See Also:
- Constant Field Values
-
INDEX_NAME
protected static final java.lang.String INDEX_NAME
- See Also:
- Constant Field Values
-
NOT_FOUND_FETCHER
protected static Mustache.VariableFetcher NOT_FOUND_FETCHER
A fetcher cached for lookups that failed to find a fetcher.
-
-
Constructor Detail
-
Template
protected Template(Template.Segment[] segs, Mustache.Compiler compiler)
-
-
Method Detail
-
execute
public java.lang.String execute(java.lang.Object context) throws MustacheExceptionExecutes 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(java.lang.Object context, java.io.Writer out) throws MustacheExceptionExecutes 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(java.lang.Object context, java.lang.Object parentContext, java.io.Writer out) throws MustacheExceptionExecutes 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.
-
indent
protected Template indent(java.lang.String indent)
-
replaceBlocks
protected Template replaceBlocks(java.util.Map<java.lang.String,Mustache.BlockSegment> blocks)
-
executeSegs
protected void executeSegs(Template.Context ctx, java.io.Writer out) throws MustacheException
- Throws:
MustacheException
-
createFragment
protected Template.Fragment createFragment(Template.Segment[] segs, Template.Context currentCtx)
-
getValue
protected java.lang.Object getValue(Template.Context ctx, java.lang.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 java.lang.Object getCompoundValue(Template.Context ctx, java.lang.String name, int line, boolean missingIsNull)
Decomposes the compound keynameinto components and resolves the value they reference.
-
getSectionValue
protected java.lang.Object getSectionValue(Template.Context ctx, java.lang.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 java.lang.Object getValueOrDefault(Template.Context ctx, java.lang.String name, int line)
Returns the value for the specified variable, or the configured default value if the variable resolves to null. SeegetValue(com.samskivert.mustache.Template.Context, java.lang.String, int, boolean).
-
getValueIn
protected java.lang.Object getValueIn(java.lang.Object data, java.lang.String name, int line)
-
checkForMissing
protected java.lang.Object checkForMissing(java.lang.String name, int line, boolean missingIsNull, java.lang.Object value)
-
isThisName
protected static boolean isThisName(java.lang.String name)
-
-