Skip to content

Commit

Permalink
Add a note about how macros can override existing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 7, 2024
1 parent 26b1dc5 commit ec282bf
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions doc/tags/macro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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 accessible by calling them 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
Expand Down

0 comments on commit ec282bf

Please sign in to comment.