Skip to content

Commit d730bb7

Browse files
committed
Fix Function vs TemplateFunction so that you can grab output
1 parent 314ad15 commit d730bb7

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,22 @@ public void run() {
8787
protected Writer handleFunction(Writer writer, Function function, Object[] scopes) {
8888
StringWriter sw = new StringWriter();
8989
runIdentity(sw);
90-
Object newtemplate = function.apply(sw.toString());
91-
if (newtemplate != null) {
92-
if (function instanceof TemplateFunction) {
90+
if (function instanceof TemplateFunction) {
91+
Object newtemplate = function.apply(sw.toString());
92+
if (newtemplate != null) {
9393
String templateText = newtemplate.toString();
9494
writer = writeTemplate(writer, templateText, scopes);
95-
} else {
96-
try {
97-
writer.write(newtemplate.toString());
98-
} catch (IOException e) {
99-
throw new MustacheException("Failed to write function result", e);
95+
}
96+
} else {
97+
try {
98+
StringWriter capture = new StringWriter();
99+
writeTemplate(capture, sw.toString(), scopes).close();
100+
Object apply = function.apply(capture.toString());
101+
if (apply != null) {
102+
writer.write(apply.toString());
100103
}
104+
} catch (IOException e) {
105+
throw new MustacheException("Failed to write function result", e);
101106
}
102107
}
103108
return writer;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.mustachejava.util.CapturingMustacheVisitor;
55
import com.github.mustachejavabenchmarks.JsonCapturer;
66
import com.github.mustachejavabenchmarks.JsonInterpreterTest;
7+
import com.google.common.base.Function;
78
import junit.framework.TestCase;
89
import org.codehaus.jackson.JsonGenerator;
910
import org.codehaus.jackson.JsonNode;
@@ -338,6 +339,39 @@ public String apply(@Nullable String s) {
338339
assertEquals("This is not interesting.", sw.toString());
339340
}
340341

342+
public void testFunctions() throws IOException {
343+
MustacheFactory c = init();
344+
Mustache m = c.compile(new StringReader("{{#f}}{{foo}}{{/f}}"), "test");
345+
{
346+
StringWriter sw = new StringWriter();
347+
m.execute(sw, new Object() {
348+
Function f = new Function<String, String>() {
349+
@Override
350+
public String apply(String s) {
351+
return s.toUpperCase();
352+
}
353+
};
354+
String foo = "bar";
355+
}).flush();
356+
assertEquals("BAR", sw.toString());
357+
}
358+
{
359+
StringWriter sw = new StringWriter();
360+
m.execute(sw, new Object() {
361+
Function f = new TemplateFunction() {
362+
@Override
363+
public String apply(String s) {
364+
return s.toUpperCase();
365+
}
366+
};
367+
String foo = "bar";
368+
String FOO = "baz";
369+
}).flush();
370+
assertEquals("baz", sw.toString());
371+
}
372+
}
373+
374+
341375
public void testComplex() throws MustacheException, IOException {
342376
StringWriter json = new StringWriter();
343377
MappingJsonFactory jf = new MappingJsonFactory();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public String apply(String input) {
108108
});
109109
put("Section", new Object() {
110110
Function lambda() {
111-
return new Function<String, String>() {
111+
return new TemplateFunction() {
112112
@Override
113113
public String apply(String input) {
114114
return input.equals("{{x}}") ? "yes" : "no";

0 commit comments

Comments
 (0)