From aa96d0d57aa0b874c29987af11407c1e8c4eea17 Mon Sep 17 00:00:00 2001 From: Mitchell Bosecke Date: Mon, 27 Jan 2014 22:12:59 -0700 Subject: [PATCH] Fixed issue where macros couldn't be called twice --- .../pebble/node/NodeMacro.java | 2 +- .../pebble/template/PebbleTemplateImpl.java | 2 +- .../mitchellbosecke/pebble/CoreTagsTest.java | 19 +++++++++++++++++-- .../resources/templates/template.macro1.peb | 2 +- .../templates/template.macroDouble.peb | 5 +++++ 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/templates/template.macroDouble.peb diff --git a/src/main/java/com/mitchellbosecke/pebble/node/NodeMacro.java b/src/main/java/com/mitchellbosecke/pebble/node/NodeMacro.java index c87b6846a..622b15189 100644 --- a/src/main/java/com/mitchellbosecke/pebble/node/NodeMacro.java +++ b/src/main/java/com/mitchellbosecke/pebble/node/NodeMacro.java @@ -45,7 +45,7 @@ public void compileGetNameMethod(Compiler compiler) { } public void compileInit(Compiler compiler) { - compiler.write("public void init(){").indent(); + compiler.write("public void init(){").indent().newline(); for (NodeExpression arg : args.getArgs()) { NodeExpressionDeclaration variableDeclaration = (NodeExpressionDeclaration) arg; diff --git a/src/main/java/com/mitchellbosecke/pebble/template/PebbleTemplateImpl.java b/src/main/java/com/mitchellbosecke/pebble/template/PebbleTemplateImpl.java index f53ca4b01..7bba2af84 100644 --- a/src/main/java/com/mitchellbosecke/pebble/template/PebbleTemplateImpl.java +++ b/src/main/java/com/mitchellbosecke/pebble/template/PebbleTemplateImpl.java @@ -119,6 +119,7 @@ protected Object getAttribute(Context context, Object object, String attribute, public void registerMacro(Macro macro) { macros.put(macro.getName(), macro); + macro.init(); } public boolean hasMacro(String macroName) { @@ -143,7 +144,6 @@ public String macro(String macroName, Context context, Object... args) throws Pe found = true; Macro macro = macros.get(macroName); - macro.init(); result = macro.call(context, args); } diff --git a/src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java b/src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java index a2ec4c442..45d4182e1 100644 --- a/src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java +++ b/src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java @@ -128,8 +128,23 @@ public void testMacro() throws PebbleException, IOException { Writer writer = new StringWriter(); template.evaluate(writer); - assertEquals(" \n", - writer.toString()); + assertEquals(" \n", writer.toString()); + } + + /** + * There was an issue where the second invokation of a macro + * did not have access to the original arguments any more. + * + * @throws PebbleException + * @throws IOException + */ + @Test + public void testMacroInvokedTwice() throws PebbleException, IOException { + PebbleTemplate template = pebble.compile("template.macroDouble.peb"); + + Writer writer = new StringWriter(); + template.evaluate(writer); + assertEquals("onetwo", writer.toString()); } /** diff --git a/src/test/resources/templates/template.macro1.peb b/src/test/resources/templates/template.macro1.peb index 14f9d647c..f8151e5f1 100644 --- a/src/test/resources/templates/template.macro1.peb +++ b/src/test/resources/templates/template.macro1.peb @@ -1,4 +1,4 @@ -{{ input("company", "forcorp", "text") }} +{{ input("company", "google", "text") }} {% macro input(name, value, type) %} {% endmacro %} \ No newline at end of file diff --git a/src/test/resources/templates/template.macroDouble.peb b/src/test/resources/templates/template.macroDouble.peb new file mode 100644 index 000000000..94ef49871 --- /dev/null +++ b/src/test/resources/templates/template.macroDouble.peb @@ -0,0 +1,5 @@ +{{ repeat('one') }} +{{ repeat('two') }} +{% macro repeat(input) %} +{{ input }} +{% endmacro %} \ No newline at end of file