From 3004ba19d44b340d392a2635d08b1a286d55aa15 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Haso=C5=88?= Date: Sat, 11 May 2013 20:04:07 +0200 Subject: [PATCH] Added support for directly call macros defined in the same template --- CHANGELOG | 1 + doc/templates.rst | 16 ++++++++++++++++ lib/Twig/ExpressionParser.php | 10 +++++++++- .../macros/auto_importing_in_same_file.test | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test diff --git a/CHANGELOG b/CHANGELOG index 0736a2b..9db9382 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.13.2 (2013-XX-XX) + * in the template can be directly called macros defined in the same template * added support for named arguments for macros * fixed fatal error that should be an exception when macro does not exist in template diff --git a/doc/templates.rst b/doc/templates.rst index c144702..81dcd35 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -505,6 +505,7 @@ Macros .. versionadded:: 1.13.2 Support for macro call with named arguments was added in Twig 1.13.2. + Support for directly call macros defined in the same template was added in Twig 1.13.2. Macros are comparable with functions in regular programming languages. They are useful to reuse often used HTML fragments to not repeat yourself. @@ -527,6 +528,21 @@ Macros can be defined in any template, and need to be "imported" via the

{{ forms.input('username') }}

+Macros defined in the same template can be directly called: + +.. code-block:: jinja + + {% macro submit(name) %} + + {% endmacro %} + +

{{ submit('Send') }}

+ +.. note:: + + If the macro name matches the name of a function, it need to be "imported" + via the :doc:`import`. + Alternatively, you can import individual macro names from a template into the current namespace via the :doc:`from` tag and optionally alias them: diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 8565f1b..09d16dd 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -323,7 +323,15 @@ class Twig_ExpressionParser return new Twig_Node_Expression_MacroCall($alias['node'], $alias['name'], $this->createArrayFromArguments($args), $line); } - $class = $this->getFunctionNodeClass($name, $line); + try { + $class = $this->getFunctionNodeClass($name, $line); + } catch (Twig_Error_Syntax $e) { + if (!$this->parser->hasMacro($name)) { + throw $e; + } + + return new Twig_Node_Expression_MacroCall(new Twig_Node_Expression_Name('_self', $line), $name, $this->createArrayFromArguments($args), $line); + } return new $class($name, $args, $line); } diff --git a/test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test b/test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test new file mode 100644 index 0000000..4ebb930 --- /dev/null +++ b/test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test @@ -0,0 +1,20 @@ +--TEST-- +Auto importing macros in the same file +--TEMPLATE-- +{% macro test(a) %} +{{ a }} +{%- endmacro %} + +{% macro foo(a) %} +foo: {{ test(a) }} +{%- endmacro %} + +{{ test('foo') }} +{{ foo('foo') }} +{{ test(a = 'bar') }} +--DATA-- +return array(); +--EXPECT-- +foo +foo: foo +bar -- 1.7.2.5