Preserve nesting with non-empty strings; tidying.
This commit is contained in:
@@ -45,8 +45,8 @@ public class Mustache
|
|||||||
* configured to a non-null value. */
|
* configured to a non-null value. */
|
||||||
public final boolean missingIsNull;
|
public final boolean missingIsNull;
|
||||||
|
|
||||||
/** If this value is true, empty string will be treated as a false value, as in
|
/** If this value is true, empty string will be treated as a false value, as in JavaScript
|
||||||
* Javascript mustache implementation, default is false. */
|
* mustache implementation. Default is false. */
|
||||||
public final boolean emptyStringIsFalse;
|
public final boolean emptyStringIsFalse;
|
||||||
|
|
||||||
/** The template loader in use during this compilation. */
|
/** The template loader in use during this compilation. */
|
||||||
@@ -104,9 +104,9 @@ public class Mustache
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a compiler that will treat empty string as a false value if parameter is true. */
|
/** Returns a compiler that will treat empty string as a false value if parameter is true. */
|
||||||
public Compiler emptyStringIsFalse(boolean emptyStringIsFalse) {
|
public Compiler emptyStringIsFalse (boolean emptyStringIsFalse) {
|
||||||
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
|
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
|
||||||
this.missingIsNull, emptyStringIsFalse, this.loader, this.collector);
|
this.missingIsNull, emptyStringIsFalse, this.loader, this.collector);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a compiler configured to use the supplied template loader to handle partials. */
|
/** Returns a compiler configured to use the supplied template loader to handle partials. */
|
||||||
@@ -122,7 +122,8 @@ public class Mustache
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Compiler (boolean escapeHTML, boolean standardsMode, String nullValue,
|
protected Compiler (boolean escapeHTML, boolean standardsMode, String nullValue,
|
||||||
boolean missingIsNull, boolean emptyStringIsFalse, TemplateLoader loader, Collector collector) {
|
boolean missingIsNull, boolean emptyStringIsFalse, TemplateLoader loader,
|
||||||
|
Collector collector) {
|
||||||
this.escapeHTML = escapeHTML;
|
this.escapeHTML = escapeHTML;
|
||||||
this.standardsMode = standardsMode;
|
this.standardsMode = standardsMode;
|
||||||
this.nullValue = nullValue;
|
this.nullValue = nullValue;
|
||||||
@@ -446,7 +447,8 @@ public class Mustache
|
|||||||
}
|
}
|
||||||
@Override protected Accumulator addCloseSectionSegment (String itag, int line) {
|
@Override protected Accumulator addCloseSectionSegment (String itag, int line) {
|
||||||
requireSameName(tag1, itag, line);
|
requireSameName(tag1, itag, line);
|
||||||
outer._segs.add(new SectionSegment(itag, super.finish(), tagLine, _compiler));
|
outer._segs.add(
|
||||||
|
new SectionSegment(itag, super.finish(), tagLine, _compiler));
|
||||||
return outer;
|
return outer;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -517,7 +519,7 @@ public class Mustache
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Compiler _compiler;
|
protected final Compiler _compiler;
|
||||||
protected final List<Template.Segment> _segs = new ArrayList<Template.Segment>();
|
protected final List<Template.Segment> _segs = new ArrayList<Template.Segment>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -604,9 +606,9 @@ public class Mustache
|
|||||||
|
|
||||||
/** A segment that represents a section. */
|
/** A segment that represents a section. */
|
||||||
protected static class SectionSegment extends BlockSegment {
|
protected static class SectionSegment extends BlockSegment {
|
||||||
public SectionSegment (String name, Template.Segment[] segs, int line, Compiler _compiler) {
|
public SectionSegment (String name, Template.Segment[] segs, int line, Compiler compiler) {
|
||||||
super(name, segs, line);
|
super(name, segs, line);
|
||||||
this._compiler = _compiler;
|
_compiler = compiler;
|
||||||
}
|
}
|
||||||
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
|
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
|
||||||
Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null
|
Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null
|
||||||
@@ -622,10 +624,8 @@ public class Mustache
|
|||||||
if ((Boolean)value) {
|
if ((Boolean)value) {
|
||||||
executeSegs(tmpl, ctx, out);
|
executeSegs(tmpl, ctx, out);
|
||||||
}
|
}
|
||||||
} else if (value instanceof String && value != null && value.equals("")) {
|
} else if (_compiler.emptyStringIsFalse && "".equals(value)) {
|
||||||
if (!_compiler.emptyStringIsFalse) {
|
// omit the section
|
||||||
executeSegs(tmpl, ctx, out);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
executeSegs(tmpl, ctx.nest(value, 0, false, false), out);
|
executeSegs(tmpl, ctx.nest(value, 0, false, false), out);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user