mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-05 15:07:11 +00:00
Add a note about how macros can override existing functions
This commit is contained in:
+16
-4
@@ -7,7 +7,7 @@ are useful to reuse template fragments to not repeat yourself.
|
||||
Macros are defined in regular templates.
|
||||
|
||||
Imagine having a generic helper template that define how to render HTML forms
|
||||
via macros (called ``forms.html``):
|
||||
via macros (called ``forms.twig``):
|
||||
|
||||
.. code-block:: html+twig
|
||||
|
||||
@@ -49,9 +49,9 @@ tag:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% import "forms.html" as forms %}
|
||||
{% import "forms.twig" as forms %}
|
||||
|
||||
The above ``import`` call imports the ``forms.html`` file (which can contain
|
||||
The above ``import`` call imports the ``forms.twig`` file (which can contain
|
||||
only macros, or a template and some macros), and import the macros as items of
|
||||
the ``forms`` local variable.
|
||||
|
||||
@@ -67,11 +67,23 @@ via the ``from`` tag:
|
||||
|
||||
.. code-block:: html+twig
|
||||
|
||||
{% from 'forms.html' import input as input_field, textarea %}
|
||||
{% from 'forms.twig' import input as input_field, textarea %}
|
||||
|
||||
<p>{{ input_field('password', '', 'password') }}</p>
|
||||
<p>{{ textarea('comment') }}</p>
|
||||
|
||||
.. caution::
|
||||
|
||||
As macros imported via ``from`` are called like functions, be careful to
|
||||
not override existing functions:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% from 'forms.twig' import input as include %}
|
||||
|
||||
{# include refers to the macro and not to the built-in "include" function #}
|
||||
{{ include() }}
|
||||
|
||||
.. tip::
|
||||
|
||||
When macro usages and definitions are in the same template, you don't need to
|
||||
|
||||
Reference in New Issue
Block a user