From abec35b833abfce5eb0ac7ce2bf2bf3462a5bebf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 5 Nov 2012 08:50:56 +0100 Subject: [PATCH] fixed macro compilation when a variable name is a PHP reserved keyword (closes #881) --- CHANGELOG | 1 + lib/Twig/Node/Macro.php | 4 ++-- .../Tests/Fixtures/macros/reserved_variables.test | 14 ++++++++++++++ test/Twig/Tests/Node/MacroTest.php | 4 ++-- 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/macros/reserved_variables.test diff --git a/CHANGELOG b/CHANGELOG index f51ea8b..bceaf2f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.11.0 (2012-XX-XX) + * fixed macro compilation when a variable name is a PHP reserved keyword * changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone * fixed bitwise operator precedences * added the template_from_string function diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 11a7d1a..bdbef27 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -31,7 +31,7 @@ class Twig_Node_Macro extends Twig_Node { $arguments = array(); foreach ($this->getNode('arguments') as $argument) { - $arguments[] = '$'.$argument->getAttribute('name').' = null'; + $arguments[] = '$_'.$argument->getAttribute('name').' = null'; } $compiler @@ -52,7 +52,7 @@ class Twig_Node_Macro extends Twig_Node $compiler ->write('') ->string($argument->getAttribute('name')) - ->raw(' => $'.$argument->getAttribute('name')) + ->raw(' => $_'.$argument->getAttribute('name')) ->raw(",\n") ; } diff --git a/test/Twig/Tests/Fixtures/macros/reserved_variables.test b/test/Twig/Tests/Fixtures/macros/reserved_variables.test new file mode 100644 index 0000000..cbfb921 --- /dev/null +++ b/test/Twig/Tests/Fixtures/macros/reserved_variables.test @@ -0,0 +1,14 @@ +--TEST-- +macro +--TEMPLATE-- +{% from _self import test %} + +{% macro test(this) -%} + {{ this }} +{%- endmacro %} + +{{ test(this) }} +--DATA-- +return array('this' => 'foo'); +--EXPECT-- +foo diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index 28364c3..b6841d3 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -43,10 +43,10 @@ class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase return array( array($node, <<env->mergeGlobals(array( - "foo" => \$foo, + "foo" => \$_foo, )); \$blocks = array(); -- 1.7.2.5