Skip to content

Commit d976e0a

Browse files
Arkinatorjknack
authored andcommitted
Fix: The context can now be manipulated when in a partial (#555)
* In partials the context can now be manipulated * Deleted trailing spaces
1 parent 53537ba commit d976e0a

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/HelperResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.HashMap;
2728
import java.util.Map.Entry;
2829

2930
import com.github.jknack.handlebars.Context;
@@ -81,7 +82,7 @@ public HelperResolver(final Handlebars handlebars) {
8182
*/
8283
protected Map<String, Object> hash(final Context context) throws IOException {
8384
if (hashSize == 0) {
84-
return Collections.emptyMap();
85+
return new HashMap<>();
8586
}
8687
Map<String, Object> result = new LinkedHashMap<String, Object>();
8788
for (Entry<String, Param> entry : hash.entrySet()) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.jknack.handlebars;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
7+
public class WriteIntoContextTest extends AbstractTest {
8+
protected void configure(final Handlebars handlebars) {
9+
handlebars.registerHelpers(new WriteIntoContextTest.SetHelperClass());
10+
}
11+
12+
@Test
13+
public void shouldBeAbleToWriteIntoContext() throws IOException {
14+
shouldCompileTo("{{set \"foo\" this}}{{foo}}", "bar", "bar");
15+
}
16+
17+
@Test
18+
public void shouldBeAbleToWriteIntoContextWhenInBlockHelper() throws IOException {
19+
shouldCompileTo("{{#with data}}{{set \"foo\" field}}{{foo}}{{/with}}", "{\"data\" : {\"field\": \"bar\"}}", "bar");
20+
}
21+
22+
@Test
23+
public void shouldBeAbleToWriteIntoContextWhenInPartial() throws IOException {
24+
shouldCompileToWithPartials("{{> partial}}", "bar",
25+
constructPartials("partial", "{{set \"foo\" this}}{{foo}}"),
26+
"bar");
27+
}
28+
29+
private Hash constructPartials(String name, String content) throws IOException {
30+
return new Hash().$(name, content);
31+
}
32+
33+
public static class SetHelperClass {
34+
public String set(String key, Object value, Options options) throws NoSuchFieldException {
35+
options.context.combine(key, value);
36+
return "";
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)