Added support for composite keys, e.g. foo.bar.baz, which extracts foo from the
context, then extracts bar from that object, then extracts baz from that.
This commit is contained in:
@@ -75,6 +75,15 @@ public class Template
|
|||||||
throw new NullPointerException("Null context for variable '" + name + "'");
|
throw new NullPointerException("Null context for variable '" + name + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we're dealing with a composite key, resolve each component and use the result to
|
||||||
|
// resolve the subsequent component and so forth
|
||||||
|
if (name.indexOf(".") != -1) {
|
||||||
|
for (String comp : name.split("\\.")) {
|
||||||
|
ctx = getValue(ctx, comp);
|
||||||
|
}
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
Key key = new Key(ctx.getClass(), name);
|
Key key = new Key(ctx.getClass(), name);
|
||||||
VariableFetcher fetcher = _fcache.get(key);
|
VariableFetcher fetcher = _fcache.get(key);
|
||||||
if (fetcher != null) {
|
if (fetcher != null) {
|
||||||
|
|||||||
@@ -135,6 +135,18 @@ public class MustacheTest
|
|||||||
execute(context("things", Arrays.asList("bar", "baz", "bif"))));
|
execute(context("things", Arrays.asList("bar", "baz", "bif"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testStructuredVariable () {
|
||||||
|
test("hello", "{{foo.bar.baz}}", new Object() {
|
||||||
|
Object foo () {
|
||||||
|
return new Object() {
|
||||||
|
Object bar = new Object() {
|
||||||
|
String baz = "hello";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
protected void test (String expected, String template, Object ctx)
|
protected void test (String expected, String template, Object ctx)
|
||||||
{
|
{
|
||||||
assertEquals(expected, Mustache.compiler().compile(template).execute(ctx));
|
assertEquals(expected, Mustache.compiler().compile(template).execute(ctx));
|
||||||
|
|||||||
Reference in New Issue
Block a user