Skip to content

Commit

Permalink
minor #2444 [TwigComponent] Update using macro docs (smnandre)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

[TwigComponent] Update using macro docs

Alternative to #2437

Commits
-------

d036498 [TwigComponent] Update using macro docs
  • Loading branch information
javiereguiluz committed Dec 12, 2024
2 parents 86d08cb + d036498 commit dec3eb3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,26 @@ You can even give the block default content. See
:ref:`Passing HTML to Components via Block <embedded-components>`
for more info.

The only limitation when defining contents inside a component using the HTML syntax
is that you cannot import macros using the ``_self`` keyword. You must always use
the full template path:
Using macros in Components
~~~~~~~~~~~~~~~~~~~~~~~~~~

Defining contents inside a component works great, but what if you want to
use a macro inside a component? Good news: you can! But there's a catch:
you cannot import macros using the ``_self`` keyword. Instead, you need to
use the full path of the template where the macro is defined:

.. code-block:: html+twig

{% macro message_formatter(message) %}
<strong>{{ message }}</strong>
{% endmacro %}

<twig:Alert>
{# ❌ this won't work #}
{% from _self import message_formatter %}

{# ✅ this works as expected #}
{% from 'some/path/template.html.twig' import message_formatter %}
{% from 'path/of/this/template.html.twig' import message_formatter %}

{{ message_formatter('...') }}
</twig:Alert>
Expand Down

0 comments on commit dec3eb3

Please sign in to comment.