Expose the context to a lambda fragment.
This allows the lambda to take the context and turn around and render it using a totally different template, if desired.
This commit is contained in:
@@ -665,16 +665,41 @@ public class MustacheTest
|
||||
}
|
||||
|
||||
@Test public void testLambdaWithContext () {
|
||||
test("a in l1, a in l2", "{{#l1}}{{a}}{{/l1}}, {{#l2}}{{a}}{{/l2}}",
|
||||
context("l1", new Mustache.Lambda() {
|
||||
public void execute (Template.Fragment frag, Writer out) throws IOException {
|
||||
frag.execute(context("a", "a in l1"), out);
|
||||
}
|
||||
}, "l2", new Mustache.Lambda() {
|
||||
public void execute (Template.Fragment frag, Writer out) throws IOException {
|
||||
frag.execute(context("a", "a in l2"), out);
|
||||
}
|
||||
}));
|
||||
test("a in l1, a in l2", "{{#l1}}{{a}}{{/l1}}, {{#l2}}{{a}}{{/l2}}", context(
|
||||
"l1", new Mustache.Lambda() {
|
||||
public void execute (Template.Fragment frag, Writer out) throws IOException {
|
||||
frag.execute(context("a", "a in l1"), out);
|
||||
}
|
||||
},
|
||||
"l2", new Mustache.Lambda() {
|
||||
public void execute (Template.Fragment frag, Writer out) throws IOException {
|
||||
frag.execute(context("a", "a in l2"), out);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Test public void testContextPokingLambda () {
|
||||
Mustache.Compiler c = Mustache.compiler();
|
||||
|
||||
class Foo { public int foo = 1; }
|
||||
final Template lfoo = c.compile("{{foo}}");
|
||||
assertEquals("1", lfoo.execute(new Foo()));
|
||||
|
||||
class Bar { public String bar = "one"; }
|
||||
final Template lbar = c.compile("{{bar}}");
|
||||
assertEquals("one", lbar.execute(new Bar()));
|
||||
|
||||
test(c, "1oneone1one!", "{{#events}}{{#renderEvent}}{{/renderEvent}}{{/events}}", context(
|
||||
"renderEvent", new Mustache.Lambda() {
|
||||
public void execute (Template.Fragment frag, Writer out) throws IOException {
|
||||
Object ctx = frag.context();
|
||||
if (ctx instanceof Foo) lfoo.execute(ctx, out);
|
||||
else if (ctx instanceof Bar) lbar.execute(ctx, out);
|
||||
else out.write("!");
|
||||
}
|
||||
},
|
||||
"events", Arrays.asList(
|
||||
new Foo(), new Bar(), new Bar(), new Foo(), new Bar(), "wtf?")));
|
||||
}
|
||||
|
||||
@Test public void testNonStandardDefaultDelims () {
|
||||
|
||||
Reference in New Issue
Block a user