Resolves issue 7 regarding proper handling of nested contexts when processing a
nested context.
This commit is contained in:
@@ -458,7 +458,9 @@ public class Mustache
|
|||||||
_template = compiler.compile(r);
|
_template = compiler.compile(r);
|
||||||
}
|
}
|
||||||
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
|
@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;
|
protected final Template _template;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,18 +37,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class Template
|
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.
|
* 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.
|
* @throws MustacheException if an error occurs while executing or writing the template.
|
||||||
@@ -60,12 +48,28 @@ public class Template
|
|||||||
return out.toString();
|
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)
|
protected Template (Segment[] segs, Mustache.Compiler compiler)
|
||||||
{
|
{
|
||||||
_segs = segs;
|
_segs = segs;
|
||||||
_compiler = compiler;
|
_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
|
* Called by executing segments to obtain the value of the specified variable in the supplied
|
||||||
* context.
|
* context.
|
||||||
|
|||||||
@@ -134,6 +134,21 @@ public class MustacheTest
|
|||||||
}), "foo inside:foo nonfoo foo", "{{bar}} {{>foo}} {{>baz}} {{bar}}", context("bar", "foo"));
|
}), "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 public void testDelimiterChange () {
|
||||||
test("foo bar baz", "{{one}} {{=<% %>=}}<%two%><%={{ }}=%> {{three}}",
|
test("foo bar baz", "{{one}} {{=<% %>=}}<%two%><%={{ }}=%> {{three}}",
|
||||||
context("one", "foo", "two", "bar", "three", "baz"));
|
context("one", "foo", "two", "bar", "three", "baz"));
|
||||||
|
|||||||
Reference in New Issue
Block a user