Skip to content

[TwigComponent][LiveComponent] Add Embedded Live Components doc #1072

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

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,36 @@ To fix this, you have two options:
}
}

.. _passing-blocks:

Passing Content (Blocks) to Components
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Passing content via blocks to Live components works completely the same way you would `pass content to Twig Components`_.
Except with one important difference: when a component is re-rendered, any variables defined only in the
"outside" template will not be available. For example, this won't work:

.. code-block:: twig

{# templates/some_page.html.twig #}
{% set message = 'Variables from the outer part of the template are only available during the initial render' %}

{% component Alert %}
{% block content %}{{ message }}{% endblock %}
{% endcomponent %}

Local variables do remain available:

.. code-block:: twig

{# templates/some_page.html.twig #}
{% component Alert %}
{% block content %}
{% set message = 'this works during re-rendering!' %}
{{ message }}
{% endblock %}
{% endcomponent %}

Advanced Functionality
----------------------

Expand Down Expand Up @@ -3127,3 +3157,4 @@ bound to Symfony's BC policy for the moment.
.. _`the traditional collection type`: https://symfony.com/doc/current/form/form_themes.html#fragment-naming-for-collections
.. _`How to Work with Form Themes`: https://symfony.com/doc/current/form/form_themes.html
.. _`Symfony's built-in form theming techniques`: https://symfony.com/doc/current/form/form_themes.html
.. _`pass content to Twig Components`: https://symfony.com/bundles/ux-twig-component/current/index.html#passing-blocks
13 changes: 9 additions & 4 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,11 @@ The ``with`` data is what's mounted on the component object.
{% endblock %}
{% endcomponent %}

.. note::
.. versionadded:: 2.11

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

Inheritance & Forwarding "Outer Blocks"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1004,8 +1006,10 @@ This requires Twig 3.7.0 or higher:

<twig:Alert{{ ...myAttributes }} />

Passing Blocks to your Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _passing-blocks:

Passing Content (Blocks) to Components
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also pass content directly to your component:

Expand Down Expand Up @@ -1121,5 +1125,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
.. _`live component`: https://symfony.com/bundles/ux-live-component/current/index.html
.. _`Vue`: https://v3.vuejs.org/guide/computed.html
.. _`Live Nested Components`: https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
.. _`Passing Blocks to Live Components`: https://symfony.com/bundles/ux-live-component/current/index.html#passing-blocks
.. _`embed tag`: https://twig.symfony.com/doc/3.x/tags/embed.html
.. _`Stimulus controller`: https://symfony.com/bundles/StimulusBundle/current/index.html