diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index cae4229..2624e76 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -458,7 +458,9 @@ public class Mustache _template = compiler.compile(r); } @Override public void execute (Template tmpl, Template.Context ctx, Writer out) { - _template.execute(ctx.data, out); + // we must take care to preserve our context rather than creating a new one, which + // would happen if we just called execute() with ctx.data + _template.executeSegs(ctx, out); } protected final Template _template; } diff --git a/src/main/java/com/samskivert/mustache/Template.java b/src/main/java/com/samskivert/mustache/Template.java index ff1c1e4..b201f56 100644 --- a/src/main/java/com/samskivert/mustache/Template.java +++ b/src/main/java/com/samskivert/mustache/Template.java @@ -37,18 +37,6 @@ import java.util.concurrent.ConcurrentHashMap; */ public class Template { - /** - * Executes this template with the given context, writing the results to the supplied writer. - * @throws MustacheException if an error occurs while executing or writing the template. - */ - public void execute (Object context, Writer out) throws MustacheException - { - Context ctx = new Context(context, null, 0, Mode.OTHER); - for (Segment seg : _segs) { - seg.execute(this, ctx, out); - } - } - /** * Executes this template with the given context, returning the results as a string. * @throws MustacheException if an error occurs while executing or writing the template. @@ -60,12 +48,28 @@ public class Template return out.toString(); } + /** + * Executes this template with the given context, writing the results to the supplied writer. + * @throws MustacheException if an error occurs while executing or writing the template. + */ + public void execute (Object context, Writer out) throws MustacheException + { + executeSegs(new Context(context, null, 0, Mode.OTHER), out); + } + protected Template (Segment[] segs, Mustache.Compiler compiler) { _segs = segs; _compiler = compiler; } + protected void executeSegs (Context ctx, Writer out) throws MustacheException + { + for (Segment seg : _segs) { + seg.execute(this, ctx, out); + } + } + /** * Called by executing segments to obtain the value of the specified variable in the supplied * context. diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index 18b5f7a..816572e 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -134,6 +134,21 @@ public class MustacheTest }), "foo inside:foo nonfoo foo", "{{bar}} {{>foo}} {{>baz}} {{bar}}", context("bar", "foo")); } + @Test public void testPartialPlusNestedContext () { + test(Mustache.compiler().withLoader(new Mustache.TemplateLoader() { + public Reader getTemplate (String name) { + if (name.equals("nested")) { + return new StringReader("{{name}}{{thing_name}}"); + } else { + return new StringReader("nonfoo"); + } + } + }), "foo((foobar)(foobaz))", "{{name}}({{#things}}({{>nested}}){{/things}})", + context("name", "foo", + "things", Arrays.asList(context("thing_name", "bar"), + context("thing_name", "baz")))); + } + @Test public void testDelimiterChange () { test("foo bar baz", "{{one}} {{=<% %>=}}<%two%><%={{ }}=%> {{three}}", context("one", "foo", "two", "bar", "three", "baz"));