Skip to content

Commit 20f2fe3

Browse files
author
Sam Pullara
committed
fix for replacing a value inside an iterable in a super template
1 parent 8f07c7c commit 20f2fe3

7 files changed

Lines changed: 58 additions & 4 deletions

File tree

compiler/src/main/java/com/github/mustachejava/Mustache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface Mustache extends Code {
1313
void append(String text);
1414

1515
/**
16-
* Shallow clone of the mustache object.
16+
* Deep clone of the mustache object.
1717
* @return
1818
*/
1919
Object clone();

compiler/src/main/java/com/github/mustachejava/codes/DefaultCode.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,29 @@ public class DefaultCode implements Code, Cloneable {
1313
// Final once init() is complete
1414
protected String appended;
1515

16+
protected Mustache mustache;
1617
protected final ObjectHandler oh;
1718
protected final String name;
1819
protected final TemplateContext tc;
19-
protected final Mustache mustache;
2020
protected final String type;
2121
protected final boolean returnThis;
2222
protected final Binding binding;
2323

2424
public Object clone() {
2525
try {
26-
return super.clone();
26+
DefaultCode code = (DefaultCode) super.clone();
27+
Code[] codes = code.getCodes();
28+
if (codes != null) {
29+
codes = codes.clone();
30+
for (int i = 0; codes != null && i < codes.length; i++) {
31+
codes[i] = (Code) codes[i].clone();
32+
}
33+
code.setCodes(codes);
34+
}
35+
if (mustache != null) {
36+
code.mustache = (Mustache) mustache.clone();
37+
}
38+
return code;
2739
} catch (CloneNotSupportedException e) {
2840
throw new MustacheException("Clone not supported");
2941
}

compiler/src/main/java/com/github/mustachejava/codes/ExtendCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public synchronized void init() {
6868
"Illegal code in extend section: " + code.getClass().getName());
6969
}
7070
}
71-
partial = (Mustache) mf.compile(partialName()).clone();
71+
Mustache original = mf.compile(partialName());
72+
partial = (Mustache) original.clone();
7273
Code[] supercodes = partial.getCodes();
7374
// recursively replace named sections with replacements
7475
partial.setCodes(replaceCodes(supercodes, replaceMap));

compiler/src/test/java/com/github/mustachejava/ExtensionTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55

66
import java.io.*;
7+
import java.util.Arrays;
78
import java.util.HashMap;
89
import java.util.Map;
910
import java.util.concurrent.ExecutionException;
@@ -77,6 +78,18 @@ public void testSubSub() throws MustacheException, IOException, ExecutionExcepti
7778
assertEquals(getContents(root, "subsub.txt"), sw.toString());
7879
}
7980

81+
@Test
82+
public void testClientMethod() throws MustacheException, IOException, ExecutionException, InterruptedException {
83+
MustacheFactory c = new DefaultMustacheFactory(root);
84+
Mustache m = c.compile("client.html");
85+
StringWriter sw = new StringWriter();
86+
Map scope = new HashMap();
87+
scope.put("reply", "TestReply");
88+
scope.put("commands", Arrays.asList("a", "b"));
89+
m.execute(sw, scope);
90+
assertEquals(getContents(root, "client.txt"), sw.toString());
91+
}
92+
8093
protected String getContents(File root, String file) throws IOException {
8194
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(root, file)),"UTF-8"));
8295
StringWriter capture = new StringWriter();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{#commands}}
2+
{{<method}}
3+
{{$method}}execute{{/method}}
4+
{{/method}}
5+
{{/commands}}
6+
{{#commands}}
7+
{{<method}}
8+
{{$method}}pipeline{{/method}}
9+
{{$return}}New reply {{reply}}{{/return}}
10+
{{/method}}
11+
{{/commands}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1:TestReply
2+
2:TestReply execute
3+
3:TestReply execute
4+
1:TestReply
5+
2:TestReply execute
6+
3:TestReply execute
7+
1:New reply TestReply
8+
2:New reply TestReply pipeline
9+
3:New reply TestReply pipeline
10+
1:New reply TestReply
11+
2:New reply TestReply pipeline
12+
3:New reply TestReply pipeline
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1:{{$return}}{{reply}}{{/return}}
2+
{{#toString}}
3+
2:{{$return}}{{reply}}{{/return}} {{$method}}method{{/method}}
4+
3:{{$return}}{{reply}}{{/return}} {{$method}}method{{/method}}
5+
{{/toString}}

0 commit comments

Comments
 (0)