Skip to content

Commit dec3eb3

Browse files
committed
minor #2444 [TwigComponent] Update using macro docs (smnandre)
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
2 parents 86d08cb + d036498 commit dec3eb3

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,26 @@ You can even give the block default content. See
365365
:ref:`Passing HTML to Components via Block <embedded-components>`
366366
for more info.
367367

368-
The only limitation when defining contents inside a component using the HTML syntax
369-
is that you cannot import macros using the ``_self`` keyword. You must always use
370-
the full template path:
368+
Using macros in Components
369+
~~~~~~~~~~~~~~~~~~~~~~~~~~
370+
371+
Defining contents inside a component works great, but what if you want to
372+
use a macro inside a component? Good news: you can! But there's a catch:
373+
you cannot import macros using the ``_self`` keyword. Instead, you need to
374+
use the full path of the template where the macro is defined:
371375

372376
.. code-block:: html+twig
373377

378+
{% macro message_formatter(message) %}
379+
<strong>{{ message }}</strong>
380+
{% endmacro %}
381+
374382
<twig:Alert>
375383
{# ❌ this won't work #}
376384
{% from _self import message_formatter %}
385+
377386
{# ✅ this works as expected #}
378-
{% from 'some/path/template.html.twig' import message_formatter %}
387+
{% from 'path/of/this/template.html.twig' import message_formatter %}
379388

380389
{{ message_formatter('...') }}
381390
</twig:Alert>

0 commit comments

Comments
 (0)