-
-
Notifications
You must be signed in to change notification settings - Fork 357
[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
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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). | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
Fetching Services | ||
----------------- | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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 :)