Skip to content

[TwigComponent] Add note in doc about composition that component live in their own context #1899

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

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
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/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,33 @@ You can even give the block default content. See
:ref:`Passing HTML to Components via Block <embedded-components>`
for more info.

Composition vs Inheritance
~~~~~~~~~~~~~~~~~~~~~~~~~~

Symfony tend to follow the best pratice of the component architecture, so it's highly recommended to use Composition instead of Inheritance to reuse code betwen component (see: https://legacy.reactjs.org/docs/composition-vs-inheritance.html).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link should use the RST syntax or the doctor ill not be happy :)


Let's take an exemple. Some component don't know their children ahead of time. This is common for components like Alert or Dialog that represent generic “boxes”. We recommend you
to use the block content to pass children directly into their output :

.. code-block:: html+twig

<div class="alert">
{% block content %}
{% endblock %}
</div>

This lets other components pass arbitrary children to them by nesting the elements :

.. code-block:: html+twig

<twig:Alert>
<twig:Icon name="success"/>
<p>Nice work!</p>
</twig:Alert>

Anything inside the <twig:Alert> tag gets passed into the Alert component as a content block. Since Alert renders {% block content %} inside a <div>, the passed elements appear in the final output.
You can go deeper on the usage of block, on this part of the :ref:`documentation <embedded-components>`

Comment on lines +345 to +366
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this require a new section ? I feel this is more or less the same as what's already explained in the Passing HTML to Components Via Blocks section, or did i missread something ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I feel like this is one of the biggest traps people are falling into. This part is more about what to choose, and the block part is more about how to do it.
I tried to add more guidelines to the doc

Fetching Services
-----------------

Expand Down Expand Up @@ -670,6 +697,10 @@ You can also add more, named blocks:
</twig:block>
</twig:Alert>

.. note::

Because your TwigComponent live inside his own specific context, you can't use the parent function inside the block of your component template.

Comment on lines +700 to +703
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe my formulation is bad but I was trying to explain the issue @Nek- had. How to you explain that in a few lines?

Copy link
Contributor

@Nek- Nek- Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should maybe say that parent refers to its own context instead. Adding that it acts like embed element may also be a nice addition.

Render these in the normal way.

.. code-block:: html+twig
Expand Down