Skip to content

Commit

Permalink
Fixed issue where macros couldn't be called twice
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Jan 28, 2014
1 parent 70d4663 commit aa96d0d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down
19 changes: 17 additions & 2 deletions src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,23 @@ public void testMacro() throws PebbleException, IOException {

Writer writer = new StringWriter();
template.evaluate(writer);
assertEquals(" <input name=\"company\" value=\"forcorp\" type=\"text\" />\n",
writer.toString());
assertEquals(" <input name=\"company\" value=\"google\" type=\"text\" />\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());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/templates/template.macro1.peb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ input("company", "forcorp", "text") }}
{{ input("company", "google", "text") }}
{% macro input(name, value, type) %}
<input name="{{ name }}" value="{{ value }}" type="{{ type }}" />
{% endmacro %}
5 changes: 5 additions & 0 deletions src/test/resources/templates/template.macroDouble.peb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ repeat('one') }}
{{ repeat('two') }}
{% macro repeat(input) %}
{{ input }}
{% endmacro %}

0 comments on commit aa96d0d

Please sign in to comment.