Added a "standards mode" based on patches from mrdon. In standards mode, we do
not search parent contexts for name bindings, nor do we support compound keys.
Also added support for {{.}} which has the same semantics as {{this}}. I must
have missed this when reading the Mustache spec.
This commit is contained in:
@@ -34,6 +34,9 @@ public class Mustache
|
||||
/** Whether or not HTML entities are escaped by default. */
|
||||
public final boolean escapeHTML;
|
||||
|
||||
/** Whether or not standards mode is enabled. */
|
||||
public final boolean standardsMode;
|
||||
|
||||
/** Compiles the supplied template into a repeatedly executable intermediate form. */
|
||||
public Template compile (String template)
|
||||
{
|
||||
@@ -48,20 +51,28 @@ 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);
|
||||
return new Compiler(escapeHTML, this.standardsMode);
|
||||
}
|
||||
|
||||
protected Compiler (boolean escapeHTML) {
|
||||
/** 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);
|
||||
}
|
||||
|
||||
protected Compiler (boolean escapeHTML, boolean standardsMode) {
|
||||
this.escapeHTML = escapeHTML;
|
||||
this.standardsMode = standardsMode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a compiler that escapes HTML by default.
|
||||
* Returns a compiler that escapes HTML by default and does not use standards mode.
|
||||
*/
|
||||
public static Compiler compiler ()
|
||||
{
|
||||
return new Compiler(true);
|
||||
return new Compiler(true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +196,7 @@ public class Mustache
|
||||
throw new MustacheException("Template ended while parsing a tag TODO");
|
||||
}
|
||||
|
||||
return new Template(accum.finish());
|
||||
return new Template(accum.finish(), compiler);
|
||||
}
|
||||
|
||||
private Mustache () {} // no instantiateski
|
||||
@@ -356,10 +367,12 @@ public class Mustache
|
||||
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
|
||||
Object value = tmpl.getValue(ctx, _name, _line);
|
||||
// TODO: configurable behavior on missing values
|
||||
if (value != null) {
|
||||
String text = String.valueOf(value);
|
||||
write(out, _escapeHTML ? escapeHTML(text) : text);
|
||||
if (value == null) {
|
||||
throw new MustacheException(
|
||||
"No key, method or field with name '" + _name + "' on line " + _line);
|
||||
}
|
||||
String text = String.valueOf(value);
|
||||
write(out, _escapeHTML ? escapeHTML(text) : text);
|
||||
}
|
||||
protected boolean _escapeHTML;
|
||||
}
|
||||
@@ -425,8 +438,7 @@ public class Mustache
|
||||
Object value = tmpl.getValue(ctx, _name, _line);
|
||||
if (value == null) {
|
||||
executeSegs(tmpl, ctx, out); // TODO: configurable behavior on missing values
|
||||
}
|
||||
if (value instanceof Iterable<?>) {
|
||||
} else if (value instanceof Iterable<?>) {
|
||||
Iterable<?> iable = (Iterable<?>)value;
|
||||
if (!iable.iterator().hasNext()) {
|
||||
executeSegs(tmpl, ctx, out);
|
||||
@@ -444,7 +456,7 @@ public class Mustache
|
||||
if (!iter.hasNext()) {
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
}
|
||||
} // TODO: fail?
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user