Skip to content

Commit

Permalink
Inline partials leak from block context to global context fix #523
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Aug 2, 2016
1 parent f9dd86b commit 7c06198
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -99,6 +100,18 @@ public Partial(final Handlebars handlebars, final Template path, final String co
this.loader = handlebars.getLoader();
}

@Override
public void before(final Context context, final Writer writer) throws IOException {
LinkedList<Map<String, Template>> partials = context.data(Context.INLINE_PARTIALS);
partials.addLast(new HashMap<String, Template>(partials.getLast()));
}

@Override
public void after(final Context context, final Writer writer) throws IOException {
LinkedList<Map<String, Template>> partials = context.data(Context.INLINE_PARTIALS);
partials.removeLast();
}

@Override
protected void merge(final Context context, final Writer writer)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public TemplateList(final Handlebars handlebars) {
*/
public boolean add(final Template child) {
nodes.add(child);
if (child instanceof VarDecorator || child instanceof BlockDecorator) {
if (child instanceof VarDecorator || child instanceof BlockDecorator
|| child instanceof Partial) {
decorators.add((BaseTemplate) child);
decorate = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.jknack.handlebars.issues;

import java.io.IOException;

import org.junit.Test;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.v4Test;

public class Hbs523 extends v4Test {

@Override
protected void configure(final Handlebars handlebars) {
super.configure(handlebars);
handlebars.deletePartialAfterMerge(true);
}
@Test
public void shouldCallPartialWithoutSideEffect() throws IOException {
String base = "text from base partial<br>\n" +
"{{#> inlinePartial}}{{/inlinePartial}}<br>";
String inherit1 = "inherit1<br>\n" +
"{{#>base}}\n" +
"{{#*inline \"inlinePartial\"}}\n" +
"inline partial defined by inherit1, called from base\n" +
"{{/inline}}\n" +
"{{/base}}";
String inherit2 = "inherit2<br>\n" +
"{{#>base}}\n" +
"{{/base}}";
String main = "main has partials:<br>\n" +
"-------------<br>\n" +
"{{>inherit1}}\n" +
"-------------<br>\n" +
"{{>inherit2}}";

shouldCompileTo(main,
$("partials", $("base", base, "inherit1", inherit1, "inherit2", inherit2)),
"main has partials:<br>\n" +
"-------------<br>\n" +
"inherit1<br>\n" +
"text from base partial<br>\n" +
"\n" +
"inline partial defined by inherit1, called from base\n" +
"<br>\n" +
"-------------<br>\n" +
"inherit2<br>\n" +
"text from base partial<br>\n" +
"<br>");
}

}

0 comments on commit 7c06198

Please sign in to comment.