Added support for obtaining variable values from fields and methods. Added a
template-local variable fetching cache that avoids reresolving the source of a variable when it remains monomorphic (e.g. it's always a field of class Foo.class or always a method named getFoo(), etc.).
This commit is contained in:
@@ -251,7 +251,7 @@ public class Mustache
|
||||
public StringSegment (String text) {
|
||||
_text = text;
|
||||
}
|
||||
@Override public void execute (Object ctx, Writer out) {
|
||||
@Override public void execute (Template tmpl, Object ctx, Writer out) {
|
||||
write(out, _text);
|
||||
}
|
||||
protected final String _text;
|
||||
@@ -271,8 +271,8 @@ public class Mustache
|
||||
super(name);
|
||||
_escapeHTML = escapeHTML;
|
||||
}
|
||||
@Override public void execute (Object ctx, Writer out) {
|
||||
Object value = getValue(ctx, _name);
|
||||
@Override public void execute (Template tmpl, Object ctx, Writer out) {
|
||||
Object value = tmpl.getValue(ctx, _name);
|
||||
// TODO: configurable behavior on missing values
|
||||
if (value != null) {
|
||||
String text = String.valueOf(value);
|
||||
@@ -288,9 +288,9 @@ public class Mustache
|
||||
super(name);
|
||||
_segs = segs;
|
||||
}
|
||||
protected void executeSegs (Object ctx, Writer out) {
|
||||
protected void executeSegs (Template tmpl, Object ctx, Writer out) {
|
||||
for (Template.Segment seg : _segs) {
|
||||
seg.execute(ctx, out);
|
||||
seg.execute(tmpl, ctx, out);
|
||||
}
|
||||
}
|
||||
protected final Template.Segment[] _segs;
|
||||
@@ -301,31 +301,31 @@ public class Mustache
|
||||
public SectionSegment (String name, Template.Segment[] segs) {
|
||||
super(name, segs);
|
||||
}
|
||||
@Override public void execute (Object ctx, Writer out) {
|
||||
Object value = getValue(ctx, _name);
|
||||
@Override public void execute (Template tmpl, Object ctx, Writer out) {
|
||||
Object value = tmpl.getValue(ctx, _name);
|
||||
if (value == null) {
|
||||
return; // TODO: configurable behavior on missing values
|
||||
}
|
||||
if (value instanceof Iterable<?>) {
|
||||
Iterable<?> iable = (Iterable<?>)value;
|
||||
for (Object elem : iable) {
|
||||
executeSegs(elem, out);
|
||||
executeSegs(tmpl, elem, out);
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
if ((Boolean)value) {
|
||||
executeSegs(ctx, out);
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
} else if (value.getClass().isArray()) {
|
||||
for (int ii = 0, ll = Array.getLength(value); ii < ll; ii++) {
|
||||
executeSegs(Array.get(value, ii), out);
|
||||
executeSegs(tmpl, Array.get(value, ii), out);
|
||||
}
|
||||
} else if (value instanceof Iterator<?>) {
|
||||
Iterator<?> iter = (Iterator<?>)value;
|
||||
while (iter.hasNext()) {
|
||||
executeSegs(iter.next(), out);
|
||||
executeSegs(tmpl, iter.next(), out);
|
||||
}
|
||||
} else {
|
||||
executeSegs(value, out);
|
||||
executeSegs(tmpl, value, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -335,28 +335,28 @@ public class Mustache
|
||||
public InvertedSectionSegment (String name, Template.Segment[] segs) {
|
||||
super(name, segs);
|
||||
}
|
||||
@Override public void execute (Object ctx, Writer out) {
|
||||
Object value = getValue(ctx, _name);
|
||||
@Override public void execute (Template tmpl, Object ctx, Writer out) {
|
||||
Object value = tmpl.getValue(ctx, _name);
|
||||
if (value == null) {
|
||||
executeSegs(ctx, out); // TODO: configurable behavior on missing values
|
||||
executeSegs(tmpl, ctx, out); // TODO: configurable behavior on missing values
|
||||
}
|
||||
if (value instanceof Iterable<?>) {
|
||||
Iterable<?> iable = (Iterable<?>)value;
|
||||
if (!iable.iterator().hasNext()) {
|
||||
executeSegs(ctx, out);
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
if (!(Boolean)value) {
|
||||
executeSegs(ctx, out);
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
} else if (value.getClass().isArray()) {
|
||||
if (Array.getLength(value) == 0) {
|
||||
executeSegs(ctx, out);
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
} else if (value instanceof Iterator<?>) {
|
||||
Iterator<?> iter = (Iterator<?>)value;
|
||||
if (!iter.hasNext()) {
|
||||
executeSegs(ctx, out);
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user