- java.lang.Object
-
- com.samskivert.mustache.Mustache.Compiler
-
-
Field Summary
Fields Modifier and Type Field Description Mustache.Collector
collector
The collector used by templates compiled with this compiler.Mustache.Delims
delims
The delimiters used by default in templates compiled with this compiler.boolean
emptyStringIsFalse
If this value is true, empty string will be treated as a false value, as in JavaScript mustache implementation.Mustache.Escaper
escaper
Handles escaping characters in substituted text.Mustache.Formatter
formatter
Handles converting objects to strings when rendering a template.Mustache.TemplateLoader
loader
The template loader in use during this compilation.boolean
missingIsNull
If this value is true, missing variables will be treated like variables that return null.java.lang.String
nullValue
A value to use when a variable resolves to null.boolean
standardsMode
Whether or not standards mode is enabled.boolean
strictSections
Whether or not to throw an exception when a section resolves to a missing value.boolean
zeroIsFalse
If this value is true, zero will be treated as a false value, as in JavaScript mustache implementation.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Compiler(boolean standardsMode, boolean strictSections, java.lang.String nullValue, boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse, Mustache.Formatter formatter, Mustache.Escaper escaper, Mustache.TemplateLoader loader, Mustache.Collector collector, Mustache.Delims delims)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Template
compile(java.io.Reader source)
Compiles the supplied template into a repeatedly executable intermediate form.Template
compile(java.lang.String template)
Compiles the supplied template into a repeatedly executable intermediate form.java.lang.String
computeNullValue(java.lang.String name)
Returns the value to use in the template for the null-valued propertyname
.Mustache.Compiler
defaultValue(java.lang.String defaultValue)
Returns a compiler that will use the given value for any variable that is missing, or otherwise resolves to null.Mustache.Compiler
emptyStringIsFalse(boolean emptyStringIsFalse)
Returns a compiler that will treat empty string as a false value if parameter is true.Mustache.Compiler
escapeHTML(boolean escapeHTML)
Returns a compiler that either does or does not escape HTML by default.private boolean
isEmptyCharSequence(java.lang.Object value)
Replaces "".equals(value).boolean
isFalsey(java.lang.Object value)
Returns true if the supplied value is "falsey".Template
loadTemplate(java.lang.String name)
Loads and compiles the templatename
using this compiler's configured template loader.Mustache.Compiler
nullValue(java.lang.String nullValue)
Returns a compiler that will use the given value for any variable that resolves to null, but will still raise an exception for variables for which an accessor cannot be found.Mustache.Compiler
standardsMode(boolean standardsMode)
Returns a compiler that either does or does not use standards mode.Mustache.Compiler
strictSections(boolean strictSections)
Returns a compiler that throws an exception when a section references a missing value (true
) or treats a missing value asfalse
(false
, the default).Mustache.Compiler
withCollector(Mustache.Collector collector)
Returns a compiler configured to use the supplied collector.Mustache.Compiler
withDelims(java.lang.String delims)
Returns a compiler configured to use the supplied delims as default delimiters.Mustache.Compiler
withEscaper(Mustache.Escaper escaper)
Configures theMustache.Escaper
used to escape substituted text.Mustache.Compiler
withFormatter(Mustache.Formatter formatter)
Configures theMustache.Formatter
used to turn objects into strings.Mustache.Compiler
withLoader(Mustache.TemplateLoader loader)
Returns a compiler configured to use the supplied template loader to handle partials.Mustache.Compiler
zeroIsFalse(boolean zeroIsFalse)
Returns a compiler that will treat zero as a false value if parameter is true.
-
-
-
Field Detail
-
standardsMode
public final boolean standardsMode
Whether or not standards mode is enabled.
-
strictSections
public final boolean strictSections
Whether or not to throw an exception when a section resolves to a missing value. If false, the section is simply omitted (or included in the case of inverse sections). If true, aMustacheException
is thrown.
-
nullValue
public final java.lang.String nullValue
A value to use when a variable resolves to null. If this value is null (which is the default null value), an exception will be thrown. IfmissingIsNull
is also true, this value will be used when a variable cannot be resolved.If the nullValue contains a substring
{{name}}
, then this substring will be replaced by name of the variable. For example, if nullValue is?{{name}}?
and the missing variable isfoo
, then string?foo?
will be used.
-
missingIsNull
public final boolean missingIsNull
-
emptyStringIsFalse
public final boolean emptyStringIsFalse
If this value is true, empty string will be treated as a false value, as in JavaScript mustache implementation. Default is false.
-
zeroIsFalse
public final boolean zeroIsFalse
If this value is true, zero will be treated as a false value, as in JavaScript mustache implementation. Default is false.
-
formatter
public final Mustache.Formatter formatter
Handles converting objects to strings when rendering a template. The default formatter usesString.valueOf(java.lang.Object)
.
-
escaper
public final Mustache.Escaper escaper
Handles escaping characters in substituted text.
-
loader
public final Mustache.TemplateLoader loader
The template loader in use during this compilation.
-
collector
public final Mustache.Collector collector
The collector used by templates compiled with this compiler.
-
delims
public final Mustache.Delims delims
The delimiters used by default in templates compiled with this compiler.
-
-
Constructor Detail
-
Compiler
protected Compiler(boolean standardsMode, boolean strictSections, java.lang.String nullValue, boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse, Mustache.Formatter formatter, Mustache.Escaper escaper, Mustache.TemplateLoader loader, Mustache.Collector collector, Mustache.Delims delims)
-
-
Method Detail
-
compile
public Template compile(java.lang.String template)
Compiles the supplied template into a repeatedly executable intermediate form.
-
compile
public Template compile(java.io.Reader source)
Compiles the supplied template into a repeatedly executable intermediate form.
-
escapeHTML
public Mustache.Compiler escapeHTML(boolean escapeHTML)
Returns a compiler that either does or does not escape HTML by default. Note: this overrides any escaper set viawithEscaper(com.samskivert.mustache.Mustache.Escaper)
.
-
standardsMode
public Mustache.Compiler standardsMode(boolean standardsMode)
Returns a compiler that either does or does not use standards mode. Standards mode disables the non-standard JMustache extensions like looking up missing names in a parent context.
-
strictSections
public Mustache.Compiler strictSections(boolean strictSections)
Returns a compiler that throws an exception when a section references a missing value (true
) or treats a missing value asfalse
(false
, the default).
-
defaultValue
public Mustache.Compiler defaultValue(java.lang.String defaultValue)
Returns a compiler that will use the given value for any variable that is missing, or otherwise resolves to null. This is likenullValue
except that it returns the supplied default for missing keys and existing keys that return null values.
-
nullValue
public Mustache.Compiler nullValue(java.lang.String nullValue)
Returns a compiler that will use the given value for any variable that resolves to null, but will still raise an exception for variables for which an accessor cannot be found. This is likedefaultValue(java.lang.String)
except that it differentiates between missing accessors, and accessors that exist but return null.- In the case of a Java object being used as a context, if no field or method can be found for a variable, an exception will be raised.
- In the case of a
Map
being used as a context, if the map does not contain a mapping for a variable, an exception will be raised. If the map contains a mapping which maps tonull
, thennullValue
is used.
-
emptyStringIsFalse
public Mustache.Compiler emptyStringIsFalse(boolean emptyStringIsFalse)
Returns a compiler that will treat empty string as a false value if parameter is true.
-
zeroIsFalse
public Mustache.Compiler zeroIsFalse(boolean zeroIsFalse)
Returns a compiler that will treat zero as a false value if parameter is true.
-
withFormatter
public Mustache.Compiler withFormatter(Mustache.Formatter formatter)
Configures theMustache.Formatter
used to turn objects into strings.
-
withEscaper
public Mustache.Compiler withEscaper(Mustache.Escaper escaper)
Configures theMustache.Escaper
used to escape substituted text.
-
withLoader
public Mustache.Compiler withLoader(Mustache.TemplateLoader loader)
Returns a compiler configured to use the supplied template loader to handle partials.
-
withCollector
public Mustache.Compiler withCollector(Mustache.Collector collector)
Returns a compiler configured to use the supplied collector.
-
withDelims
public Mustache.Compiler withDelims(java.lang.String delims)
Returns a compiler configured to use the supplied delims as default delimiters.- Parameters:
delims
- a string of the formAB CD
orA D
where A and B are opening delims and C and D are closing delims.
-
computeNullValue
public java.lang.String computeNullValue(java.lang.String name)
Returns the value to use in the template for the null-valued propertyname
. SeenullValue
for more details.
-
isFalsey
public boolean isFalsey(java.lang.Object value)
Returns true if the supplied value is "falsey". IfemptyStringIsFalse
is true, then empty strings are considered falsey. IfzeroIsFalse
is true, then zero values are considered falsey.
-
isEmptyCharSequence
private boolean isEmptyCharSequence(java.lang.Object value)
Replaces "".equals(value). E.g. only not null values with length 0.
-
loadTemplate
public Template loadTemplate(java.lang.String name) throws MustacheException
Loads and compiles the templatename
using this compiler's configured template loader. Note that this does no caching: the caller should cache the loaded template if they expect to use it multiple times.- Returns:
- the compiled template.
- Throws:
MustacheException
- if the template could not be loaded (due to I/O exception) or compiled (due to syntax error, etc.).
-
-