diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index b8fbd08..e95e237 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -39,9 +39,10 @@ public class Mustache /** 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. If {@link #missingIsNull} 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="?{{name}}?" - * and the missing variable is "foo", then string "?foo?" will be used. */ + * + *
If the nullValue contains a substring {@code {{name}}}, then this substring will be + * replaced by name of the variable. For example, if nullValue is {@code ?{{name}}?} and + * the missing variable is {@code foo}, then string {@code ?foo?} will be used.
*/ public final String nullValue; /** If this value is true, missing variables will be treated like variables that return @@ -125,6 +126,12 @@ public class Mustache this.missingIsNull, this.emptyStringIsFalse, this.loader, collector); } + /** Returns the value to use in the template for the null-valued property {@code name}. See + * {@link #nullValue} for more details. */ + public String computeNullValue (String name) { + return (nullValue == null) ? null : nullValue.replace("{{name}}", name); + } + protected Compiler (boolean escapeHTML, boolean standardsMode, String nullValue, boolean missingIsNull, boolean emptyStringIsFalse, TemplateLoader loader, Collector collector) { diff --git a/src/main/java/com/samskivert/mustache/Template.java b/src/main/java/com/samskivert/mustache/Template.java index 4929b34..445c42d 100644 --- a/src/main/java/com/samskivert/mustache/Template.java +++ b/src/main/java/com/samskivert/mustache/Template.java @@ -170,16 +170,7 @@ public class Template // getValue will raise MustacheException if a variable cannot be resolved and missingIsNull // is not configured; so we're safe to assume that any null that makes it up to this point // can be converted to nullValue - return (value == null) ? substituteNullValue(name) : value; - } - - private String substituteNullValue(String name) { - String SUBST = "\\{\\{name\\}\\}"; // escaping needed for regex - String result = _compiler.nullValue; - if (result != null) { - result = result.replaceAll(SUBST, name); - } - return result; + return (value == null) ? _compiler.computeNullValue(name) : value; } protected Object getValueIn (Object data, String name, int line)