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:
Michael Bayne
2011-04-15 12:35:33 -07:00
parent 5d59eada29
commit 15ff48eabb
3 changed files with 29 additions and 30 deletions
@@ -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);
@@ -126,10 +126,14 @@ public class Template
return null;
}
protected Object getValueWithDefault (Context ctx, String name, int line)
/**
* Returns the value for the specified variable, or the configured default value if the
* variable resolves to null. See {@link #getValue}.
*/
protected Object getValueOrDefault (Context ctx, String name, int line)
{
Object value = getValue(ctx, name, line);
return value != null? value : _compiler.missingVariableValue;
return (value == null) ? _compiler.defaultValue : value;
}
protected Object getValueIn (Object data, String name, int line)
@@ -153,12 +153,12 @@ public class MustacheTest
execute(context("a", "<b>")));
}
@Test(expected = MustacheParseException.class)
@Test(expected=MustacheParseException.class)
public void testDanglingTag () {
Mustache.compiler().escapeHTML(true).compile("{{a").execute(context("a", "<b>"));
}
@Test(expected = MustacheParseException.class)
@Test(expected=MustacheParseException.class)
public void testInvalidUnescapeHTML () {
Mustache.compiler().escapeHTML(true).compile("{{{a}}").execute(context("a", "<b>"));
}
@@ -276,7 +276,7 @@ public class MustacheTest
assertEquals(":bar:", result);
}
@Test(expected = MustacheException.class)
@Test(expected=MustacheException.class)
public void testStandardsModeWithNoParentContextSearching () {
String tmpl = "{{#parent}}foo{{parentProperty}}bar{{/parent}}";
String result = Mustache.compiler().standardsMode(true).compile(tmpl).
@@ -284,25 +284,19 @@ public class MustacheTest
"parentProperty", "bar"));
}
@Test(expected = MustacheException.class)
@Test(expected=MustacheException.class)
public void testMissingValue () {
String tmpl = "{{ missing }} {{ notmissing }}";
Mustache.compiler().compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
test("n/a", "{{missing}} {{notmissing}}", context("notmissing", "bar"));
}
@Test public void testMissingValueWithDefault () {
String tmpl = "{{ missing }}{{ notmissing }}";
String result = Mustache.compiler().missingVariableValue("").compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
assertEquals("bar", result);
test(Mustache.compiler().defaultValue(""),
"bar", "{{missing}}{{notmissing}}", context("notmissing", "bar"));
}
@Test public void testMissingValueWithDefaultNonEmptyString () {
String tmpl = "{{ missing }}{{ notmissing }}";
String result = Mustache.compiler().missingVariableValue("foo").compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
assertEquals("foobar", result);
test(Mustache.compiler().defaultValue("foo"),
"foobar", "{{missing}}{{notmissing}}", context("notmissing", "bar"));
}
protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx)