From: Martin HasoĊˆ Date: Sat, 11 May 2013 18:04:07 +0000 (+0200) Subject: Added support for directly call macros defined in the same template X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=3004ba19d44b340d392a2635d08b1a286d55aa15;p=web%2Fkonrad%2Ftwig.git Added support for directly call macros defined in the same template --- 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