Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;

import com.github.jknack.handlebars.Context;
Expand Down Expand Up @@ -81,7 +82,7 @@ public HelperResolver(final Handlebars handlebars) {
*/
protected Map<String, Object> hash(final Context context) throws IOException {
if (hashSize == 0) {
return Collections.emptyMap();
return new HashMap<>();
}
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Entry<String, Param> entry : hash.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.jknack.handlebars;

import org.junit.Test;

import java.io.IOException;

public class WriteIntoContextTest extends AbstractTest {
protected void configure(final Handlebars handlebars) {
handlebars.registerHelpers(new WriteIntoContextTest.SetHelperClass());
}

@Test
public void shouldBeAbleToWriteIntoContext() throws IOException {
shouldCompileTo("{{set \"foo\" this}}{{foo}}", "bar", "bar");
}

@Test
public void shouldBeAbleToWriteIntoContextWhenInBlockHelper() throws IOException {
shouldCompileTo("{{#with data}}{{set \"foo\" field}}{{foo}}{{/with}}", "{\"data\" : {\"field\": \"bar\"}}", "bar");
}

@Test
public void shouldBeAbleToWriteIntoContextWhenInPartial() throws IOException {
shouldCompileToWithPartials("{{> partial}}", "bar",
constructPartials("partial", "{{set \"foo\" this}}{{foo}}"),
"bar");
}

private Hash constructPartials(String name, String content) throws IOException {
return new Hash().$(name, content);
}

public static class SetHelperClass {
public String set(String key, Object value, Options options) throws NoSuchFieldException {
options.context.combine(key, value);
return "";
}
}
}