Skip to content

Commit c34455d

Browse files
committed
[TwigComponent][LiveComponent] Add Embedded Live Components doc
1 parent a3a1ae9 commit c34455d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,40 @@ The parent-child system is *smart*. And with a few tricks
24532453
(:ref:`such as the key prop for lists of embedded components <rendering-quirks-with-list-of-embedded-components>`),
24542454
you can make it behave exactly like you need.
24552455

2456+
.. _passing-blocks:
2457+
2458+
Passing Blocks to Components
2459+
----------------------------
2460+
2461+
Passing blocks to Live components works completely the same way you would `pass blocks for Twig Components`_. Except...
2462+
2463+
When re-rendering a Live component, only the template for thàt component is rendered. The blocks defined in the
2464+
"parent" template are grabbed via the smart Twig templating system, but the context from the parent template is *NOT*
2465+
available during re-rendering. So, keep in mind to not refer to any outer context variables within blocks of a live component.
2466+
2467+
This won't work:
2468+
2469+
.. code-block:: twig
2470+
2471+
{# templates/some_page.html.twig #}
2472+
{% set message = 'Outer context only works on first render' %}
2473+
2474+
{% component Alert %}
2475+
{% block content %}{{ message }}{% endblock %}
2476+
{% endcomponent %}
2477+
2478+
Local variables do remain available:
2479+
2480+
.. code-block:: twig
2481+
2482+
{# templates/some_page.html.twig #}
2483+
{% component Alert %}
2484+
{% block content %}
2485+
{% set message = 'this works during re-rendering!' %}
2486+
{{ message }}
2487+
{% endblock %}
2488+
{% endcomponent %}
2489+
24562490
.. _child-component-independent-rerender:
24572491

24582492
Each component re-renders independent of one another
@@ -3127,3 +3161,4 @@ bound to Symfony's BC policy for the moment.
31273161
.. _`the traditional collection type`: https://symfony.com/doc/current/form/form_themes.html#fragment-naming-for-collections
31283162
.. _`How to Work with Form Themes`: https://symfony.com/doc/current/form/form_themes.html
31293163
.. _`Symfony's built-in form theming techniques`: https://symfony.com/doc/current/form/form_themes.html
3164+
.. _`pass blocks for Twig Components`: https://symfony.com/bundles/ux-twig-component/current/index.html#passing-blocks

src/TwigComponent/doc/index.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,11 @@ The ``with`` data is what's mounted on the component object.
830830
{% endblock %}
831831
{% endcomponent %}
832832

833-
.. note::
833+
.. versionadded:: 2.11
834834

835-
The ``{% component %}`` syntax *cannot* currently be used with LiveComponents.
835+
The ``{% component %}`` syntax can also be used with LiveComponents since 2.11.
836+
However, there are some caveats related to the context between parent and child
837+
components during re-rending. Read `Passing Blocks to Live Components`_.
836838

837839
Inheritance & Forwarding "Outer Blocks"
838840
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1121,5 +1123,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
11211123
.. _`live component`: https://symfony.com/bundles/ux-live-component/current/index.html
11221124
.. _`Vue`: https://v3.vuejs.org/guide/computed.html
11231125
.. _`Live Nested Components`: https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
1126+
.. _`Passing Blocks to Live Components`: https://symfony.com/bundles/ux-live-component/current/index.html#passing-blocks
11241127
.. _`embed tag`: https://twig.symfony.com/doc/3.x/tags/embed.html
11251128
.. _`Stimulus controller`: https://symfony.com/bundles/StimulusBundle/current/index.html

0 commit comments

Comments
 (0)