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

Conversation

WebMamba
Copy link
Contributor

@WebMamba WebMamba commented Jun 8, 2024

Q A
Bug fix? no
New feature? no
Issues Fix #1875
License MIT

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

You can have components inside your components! This is great! However, be aware that every component lives in its own context. This means that your child component does not know that it is inside a parent component; you cannot use any method like parent() inside the child component to access the parent. You should keep your components independent of the context. This rule is really important to respect, as it will guarantee that your components will always work the same way regardless of where you use them. If you need to access some parent data, you should pass properties down to your child or use events.
Copy link
Member

Choose a reason for hiding this comment

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

Nice !

Two small comments:

  • maybe split in two lines, to emphasis some words, and improve even more readability ?
  • i think we want to add a bit of nuance here or you may frighten people 😆

Instead of

You should keep your components independent of the context

Maybe

You should try to keep (...)

or

It is generally recommanded to ...

Same for the last sentence ?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's more like you must. Actually using the context is just unsupported. And I slightly disagree with the tone of this piece of documentation that suggest composition to explain inheritance does not work.

IMO it should be named something like Composition enforced if you want to get people straight to your point 😉 .

Copy link
Member

Choose a reason for hiding this comment

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

In fact it's a bit more complicated than that.

There is a difference between anonymous and class-based components, there is a difference betwen "include" and "embed" components, and i don't even start with live ones haha

But this works perfectly

    {% set aa = 'AA' %}

    <twig:Browser>

        {{ dump(aa) }}

    </twig:Browser>
Capture d’écran 2024-06-09 à 14 18 56

A component should not know an outside one existence.

There it's something else :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, it just feels weird to me to have an example unrelated to the comment following then.

Copy link
Member

Choose a reason for hiding this comment

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

My point was only that I prefer to avoid binary formulations, as things in life are rarely that simple, and few things in those components are that clear-cut. :)

@smnandre smnandre added docs Improvements or additions to documentation labels Jun 8, 2024
@WebMamba
Copy link
Contributor Author

Hey @smnandre and @Nek- I completely changed the strategy of this PR, and the ton! Tell me if you think this is better now! 😁

Comment on lines +700 to +703
.. note::

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

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.

Comment on lines +345 to +366
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>`

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

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation Status: Needs Review Needs to be reviewed TwigComponent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TwigComponent] Parent function does not work in the content of a twig component
4 participants