Some tidying up, name changes and clarifications. I went with "defaultValue"
rather than "missingVariableValue" because the value is used any time a
variable resolves to null, not necessarily just when it is missing. If you
have a context like:
new Object() {
String foo = null;
}
and reference {{foo}} in your template, foo is not missing, per se, but the
default value will still be used for it.
This commit is contained in:
@@ -38,8 +38,9 @@ public class Mustache
|
||||
/** Whether or not standards mode is enabled. */
|
||||
public final boolean standardsMode;
|
||||
|
||||
/** Replace missnig values with this value - if null will throw an exception on missing values. */
|
||||
public final String missingVariableValue;
|
||||
/** A value to use when a variable cannot be resolved, or resolves to null. If the default
|
||||
* value is null (which is the default default value), an exception will be thrown. */
|
||||
public final String defaultValue;
|
||||
|
||||
/** The template loader in use during this compilation. */
|
||||
public final TemplateLoader loader;
|
||||
@@ -56,32 +57,32 @@ public class Mustache
|
||||
|
||||
/** Returns a compiler that either does or does not escape HTML by default. */
|
||||
public Compiler escapeHTML (boolean escapeHTML) {
|
||||
return new Compiler(escapeHTML, this.standardsMode, this.missingVariableValue, this.loader);
|
||||
return new Compiler(escapeHTML, this.standardsMode, this.defaultValue, this.loader);
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
public Compiler standardsMode (boolean standardsMode) {
|
||||
return new Compiler(this.escapeHTML, standardsMode, this.missingVariableValue, this.loader);
|
||||
return new Compiler(this.escapeHTML, standardsMode, this.defaultValue, this.loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a compiler that will replace missing variables with the given value
|
||||
**/
|
||||
public Compiler missingVariableValue (String missingVariableValue) {
|
||||
return new Compiler(this.escapeHTML, this.standardsMode, missingVariableValue, this.loader);
|
||||
/** Returns a compiler that will use the given value for any variable that is missing, or
|
||||
* otherwise resolves to null. */
|
||||
public Compiler defaultValue (String defaultValue) {
|
||||
return new Compiler(this.escapeHTML, this.standardsMode, defaultValue, this.loader);
|
||||
}
|
||||
|
||||
/** Returns a compiler configured to use the supplied template loader to handle partials. */
|
||||
public Compiler withLoader (TemplateLoader loader) {
|
||||
return new Compiler(this.escapeHTML, this.standardsMode, this.missingVariableValue, loader);
|
||||
return new Compiler(this.escapeHTML, this.standardsMode, this.defaultValue, loader);
|
||||
}
|
||||
|
||||
protected Compiler (boolean escapeHTML, boolean standardsMode, String missingVariableValue, TemplateLoader loader) {
|
||||
protected Compiler (boolean escapeHTML, boolean standardsMode, String defaultValue,
|
||||
TemplateLoader loader) {
|
||||
this.escapeHTML = escapeHTML;
|
||||
this.standardsMode = standardsMode;
|
||||
this.missingVariableValue = missingVariableValue;
|
||||
this.defaultValue = defaultValue;
|
||||
this.loader = loader;
|
||||
}
|
||||
}
|
||||
@@ -479,7 +480,7 @@ public class Mustache
|
||||
_escapeHTML = escapeHTML;
|
||||
}
|
||||
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
|
||||
Object value = tmpl.getValueWithDefault(ctx, _name, _line);
|
||||
Object value = tmpl.getValueOrDefault(ctx, _name, _line);
|
||||
if (value == null) {
|
||||
throw new MustacheException(
|
||||
"No key, method or field with name '" + _name + "' on line " + _line);
|
||||
|
||||
Reference in New Issue
Block a user