Merge pull request #22 from tominsam/master
allow inner null values to mask parent context values
This commit is contained in:
@@ -41,7 +41,11 @@ public abstract class BasicCollector implements Mustache.Collector
|
|||||||
|
|
||||||
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
|
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
|
||||||
public Object get (Object ctx, String name) throws Exception {
|
public Object get (Object ctx, String name) throws Exception {
|
||||||
return ((Map<?,?>)ctx).get(name);
|
if (((Map<?,?>)ctx).containsKey(name)) {
|
||||||
|
return ((Map<?,?>)ctx).get(name);
|
||||||
|
} else {
|
||||||
|
return Template.NO_FETCHER_FOUND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -141,10 +141,6 @@ public class Template
|
|||||||
Object value = getValueIn(ctx.data, name, line);
|
Object value = getValueIn(ctx.data, name, line);
|
||||||
if (value == NO_FETCHER_FOUND) {
|
if (value == NO_FETCHER_FOUND) {
|
||||||
// preserve variableMissing
|
// preserve variableMissing
|
||||||
} else if (value == null) {
|
|
||||||
// we found a fetcher, and it returned null; so we keep searching our parents, but
|
|
||||||
// we won't freak out about a missing variable if we have a nullValue configured
|
|
||||||
variableMissing = false;
|
|
||||||
} else {
|
} else {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -356,6 +356,13 @@ public class MustacheTest
|
|||||||
"things", Arrays.asList(context("name", "bar"), context("name", "baz"))));
|
"things", Arrays.asList(context("name", "bar"), context("name", "baz"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testShadowedContextWithNull () {
|
||||||
|
test(Mustache.compiler().nullValue("(null)"),
|
||||||
|
"outer(null)", "{{foo}}{{#inner}}{{foo}}{{/inner}}",
|
||||||
|
context("foo", "outer", "inner", context("foo", null))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void testFirst () {
|
@Test public void testFirst () {
|
||||||
test("foo|bar|baz", "{{#things}}{{^-first}}|{{/-first}}{{this}}{{/things}}",
|
test("foo|bar|baz", "{{#things}}{{^-first}}|{{/-first}}{{this}}{{/things}}",
|
||||||
context("things", Arrays.asList("foo", "bar", "baz")));
|
context("things", Arrays.asList("foo", "bar", "baz")));
|
||||||
|
|||||||
Reference in New Issue
Block a user