- java.lang.Object
-
- com.samskivert.mustache.Template.Fragment
-
- Enclosing class:
- Template
public abstract class Template.Fragment extends java.lang.Object
Encapsulates a fragment of a template that is passed to a lambda. The fragment is bound to the variable context that was in effect at the time the lambda was called.
-
-
Constructor Summary
Constructors Constructor Description Fragment()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract java.lang.Object
context()
Returns the context object in effect for this fragment.abstract java.lang.Object
context(int n)
Likecontext()
btu returns then
th parent context object.java.lang.String
decompile()
Decompiles the template inside this lamdba and returns an approximation of the original template from which it was parsed.abstract java.lang.StringBuilder
decompile(java.lang.StringBuilder into)
Decompiles this fragment intointo
.java.lang.String
execute()
Executes this fragment and returns its result as a string.abstract void
execute(java.io.Writer out)
Executes this fragment; writes its result toout
.java.lang.String
execute(java.lang.Object context)
Executes this fragment with the provided context; returns its result as a string.abstract void
execute(java.lang.Object context, java.io.Writer out)
Executes this fragment with the provided context; writes its result toout
.abstract void
executeTemplate(Template tmpl, java.io.Writer out)
Executestmpl
using this fragment's bound context.
-
-
-
Method Detail
-
execute
public abstract void execute(java.io.Writer out)
Executes this fragment; writes its result toout
.
-
execute
public abstract void execute(java.lang.Object context, java.io.Writer out)
Executes this fragment with the provided context; writes its result toout
. The provided context will be nested in the fragment's bound context.
-
executeTemplate
public abstract void executeTemplate(Template tmpl, java.io.Writer out)
Executestmpl
using this fragment's bound context. This allows a lambda to resolve its fragment to a dynamically loaded template and then run that template with the same context as the lamda, allowing a lambda to act as a 'late bound' included template, i.e. you can decide which template to include based on information in the context.
-
execute
public java.lang.String execute()
Executes this fragment and returns its result as a string.
-
execute
public java.lang.String execute(java.lang.Object context)
Executes this fragment with the provided context; returns its result as a string. The provided context will be nested in the fragment's bound context.
-
context
public abstract java.lang.Object context()
Returns the context object in effect for this fragment. The actual type of the object depends on the structure of the data passed to the top-level template. You know where your lambdas are executed, so you know what type to which to cast the context in order to inspect it (be that aMap
or a POJO or something else).
-
context
public abstract java.lang.Object context(int n)
Likecontext()
btu returns then
th parent context object.0
returns the same value ascontext()
,1
returns the parent context,2
returns the grandparent and so forth. Note that if you request a parent that does not exist an exception will be thrown. You should only use this method when you know your lambda is run consistently in a context with a particular lineage.
-
decompile
public java.lang.String decompile()
Decompiles the template inside this lamdba and returns an approximation of the original template from which it was parsed. This is not the exact character for character representation because the original text is not preserved because that would incur a huge memory penalty for all users of the library when the vast majority of them do not call decompile.Limitations:
- Whitespace inside tags is not preserved: i.e.
{{ foo.bar }}
becomes{{foo.bar}}
. - If the delimiters are changed by the template, those are not preserved.
The delimiters configured on the
Compiler
are used for all decompilation.
This feature is meant to enable use of lambdas for i18n such that you can recover the contents of a lambda (so long as they're simple) to use as the lookup key for a translation string. For example:
{{#i18n}}Hello {{user.name}}!{{/i18n}}
can be sent to ani18n
lambda which can usedecompile
to recover the textHello {{user.name}}!
to be looked up in a translation dictionary. The translated fragment could then be compiled and cached and then executed in lieu of the original fragment usingcontext()
. - Whitespace inside tags is not preserved: i.e.
-
decompile
public abstract java.lang.StringBuilder decompile(java.lang.StringBuilder into)
Decompiles this fragment intointo
. Seedecompile()
.- Returns:
into
for call chaining.
-
-