Skip to content

[TwigComponent] Add documentation about passing blocks to embedded components #985

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 9 commits into from
Jul 11, 2023

Conversation

sneakyvv
Copy link
Contributor

@sneakyvv sneakyvv commented Jul 2, 2023

Q A
Bug fix? no
New feature? no
Tickets Fix #...
License MIT

This adds documentation of the feature added in #920

@sneakyvv sneakyvv changed the title Add documentation about passing blocks to embedded components [TwigComponent] Add documentation about passing blocks to embedded components Jul 2, 2023
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

Parts are a bit more technically worded than I’d like - and I dropped you an idea on slack, but this is very nice: you chose excellent examples to show off what’s possible. Thanks!

@@ -832,6 +832,140 @@ The ``with`` data is what's mounted on the component object.

Embedded components *cannot* currently be used with LiveComponents.

Passing blocks to 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.

Uppercase Blocks, Embedded and Components


.. versionadded:: 2.10

The `outerBlocks` variable has been added since 2.10.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The `outerBlocks` variable has been added since 2.10.
The ``outerBlocks`` variable was added in 2.10.

{% endcomponent %}

What if this SuccessAlert component wants to make the content of its nested Alert component configurable?
Well, you can use the special `outerBlocks` variable for those cases. This makes it possible to refer
Copy link
Member

Choose a reason for hiding this comment

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

In rst, unlike markdown, all technical words have 2 “ticks” around them, not just 1

Look mom, I'm a complex component!
{% endcomponent %}

Note that to pass a block multiple components down, each component would need to pass it along.
Copy link
Member

Choose a reason for hiding this comment

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

I’m not sure about this part - it seems obvious enough to me, and the code below REALLY makes this look complex. I vote for mentioning this perhaps, but not showing it.

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 guess you're right. Essentially, it was kinda explaining how blocks work, which is not the scope of this documentation.

{# templates/target.html.twig #}
<div>{% block foo %}default content for foo{% endblock %}</div>

Context of 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.

Uppercase everything except of


Every embedded component can be viewed as another template inside a template, hence the name.
Or in other words, in the other direction, it's as if the embedded component's blocks are copy-pasted
into that component's template. This has a few interesting consequences.
Copy link
Member

Choose a reason for hiding this comment

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

into the target component’s template

Or in other words, in the other direction, it's as if the embedded component's blocks are copy-pasted
into that component's template. This has a few interesting consequences.

First of all, although `this` is referring to the component of the template into which an (embedded)
Copy link
Member

Choose a reason for hiding this comment

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

First, although this refers to…


First of all, although `this` is referring to the component of the template into which an (embedded)
component is nested, *INSIDE* an embedded component it is referring to the *embedded* component,
*NOT* the component into which it's being nested.
Copy link
Member

Choose a reason for hiding this comment

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

The wording on this is really tricky: nested vs embedded. Maybe say less and let the code do the talking.


.. code-block:: twig

{# templates/successAlert.html.twig #}
Copy link
Member

Choose a reason for hiding this comment

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

SuccessAlert.html.twig - I think that’s in a few places.

@weaverryan
Copy link
Member

Hey! I just did a quick proof and edit of the docs so far - a team effort to explain these important concepts on a simple way. I've tried to remove the wording "embedded component". Even I mix up "embedded" and "nested" in my mind. It's not as succinct, but saying "when you pass a block to a component" instead of "when you render an embedded component" I think is much more clear.

@sneakyvv
Copy link
Contributor Author

sneakyvv commented Jul 5, 2023

Hey! I just did a quick proof and edit of the docs so far - a team effort to explain these important concepts on a simple way. I've tried to remove the wording "embedded component". Even I mix up "embedded" and "nested" in my mind. It's not as succinct, but saying "when you pass a block to a component" instead of "when you render an embedded component" I think is much more clear.

Love it. Indeed I also always have to think twice to correctly separate nested vs embedded components. It took a while to get this terminology right in my head, but it will take perhaps equally long to stop referring to them as embedded components :-)
Perhaps on a technical level, calling them that is actually still the most correct, especially seeing their similarity to the embed syntax, and even more so since the ComponentNode is in fact extending the EmbedNode. 🤓
But again, I'm all for simplicity and even more for clarity in the documentation!

@weaverryan
Copy link
Member

Perhaps on a technical level, calling them that is actually still the most correct

I think so yes. So we may sometimes still use that term - but let's see if we can avoid it for the "outside" world :)

@sneakyvv
Copy link
Contributor Author

sneakyvv commented Jul 6, 2023

@weaverryan As discussed on Slack, I also allowed references to outer blocks from with a regular (non-nested) component.
Note that these blocks probably only make sense within an extending template, because otherwise the block would be rendered where it is defined ànd where it is referred as an outerblock. It even that might be desirable in some specific use cases, who knows 🤷

@sneakyvv sneakyvv force-pushed the embedded-components-doc branch 2 times, most recently from 10ecefd to 111ac3a Compare July 6, 2023 20:56
@sneakyvv sneakyvv force-pushed the embedded-components-doc branch from 2a2accb to ae5a458 Compare July 9, 2023 20:15
@weaverryan
Copy link
Member

Thanks Bart!

@weaverryan weaverryan merged commit 5e41864 into symfony:2.x Jul 11, 2023
weaverryan added a commit that referenced this pull request Jul 17, 2023
This PR was merged into the 2.x branch.

Discussion
----------

Fix typo in outerBlocks doc

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| Tickets       |
| License       | MIT

I noticed the recently merged doc from #985 had a small typo in one of the snippets.

Commits
-------

1f45fad Fix typo in outerBlocks doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants