java.lang.Object
com.samskivert.mustache.Template.Fragment
- Enclosing class:
Template
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 -
Method Summary
Modifier and TypeMethodDescriptionabstract Object
context()
Returns the context object in effect for this fragment.abstract Object
context
(int n) Likecontext()
btu returns then
th parent context object.Decompiles the template inside this lamdba and returns an approximation of the original template from which it was parsed.abstract StringBuilder
decompile
(StringBuilder into) Decompiles this fragment intointo
.execute()
Executes this fragment and returns its result as a string.abstract void
Executes this fragment; writes its result toout
.Executes this fragment with the provided context; returns its result as a string.abstract void
Executes this fragment with the provided context; writes its result toout
.abstract void
executeTemplate
(Template tmpl, Writer out) Executestmpl
using this fragment's bound context.
-
Constructor Details
-
Fragment
public Fragment()
-
-
Method Details
-
execute
Executes this fragment; writes its result toout
. -
execute
Executes this fragment with the provided context; writes its result toout
. The provided context will be nested in the fragment's bound context. -
executeTemplate
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
Executes this fragment and returns its result as a string. -
execute
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
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
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
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
Mustache.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
Decompiles this fragment intointo
. Seedecompile()
.- Returns:
into
for call chaining.
-