Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a note about how macros can override existing functions #4175

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
Expand Down