-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
[TwigComponent] new twig_component tag and slot #873
[TwigComponent] new twig_component tag and slot #873
Conversation
I haven't looked too deep yet but one initial question: does |
I haven’t looked at the code yet, but yaaaaay!!! Is this a pretty close port of the other twig components library? Did the “slot attributes” also come along? Any complications or concerns so far? Thank you 💯! |
No, but is it really something we want we slot approach?
Yes I started from the code: https://github.com/giorgiopogliani/twig-components and I tweak some parts to add our TwigComponent logic
Yes, I keep it. I think it's a cool feature
I just need to try that on a bigger project, I just made some try on some demo projects yet. |
I'm... not sure... We have an example in the docs using it but it is contrived. (I think this particular example can be solved with slot attributes) |
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.
Can we add a check to prevent people to create a component named slot
?
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.
Yup, let’s roll with this :). Tests and playing with this will likely uncover any issues.
public function metadataForTwigComponent(string $name): ?ComponentMetadata | ||
{ | ||
if (!$config = $this->config[$name] ?? null) { | ||
return null; |
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.
You want to return null here now instead of an exception? Is that for the Antonio’ anonymous components?
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.
Yes exactly! If we don't find a corresponding component then we try to render it as anonymous
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.
What about the ability to distinguish attributes from properties for anon components as discussed here?
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.
Sorry, I missed your comment. If you look at the test I used |default to reproduce the behavior we discussed. But maybe I can add this tag in a follow-up PR if this one gets merged.
Great work! |
My answer is… I have no idea! It would be cool to setup some really complex pages and profile to find out if there are any bottlenecks. And sure, if it turns out those bottlenecks could best be alleviated with how the twig code is complied, that is possible to address (likely challenging, but possible). |
Good to know that it's possible at least! Thanks |
1aa4129
to
59b8f78
Compare
Hey all! In #863 @weaverryan suggest not choosing between slots and blocks and making these two concepts work together! So I made some changes in this PR and now you can use slot and block in your components! 😁 I have to confess it spin my head! I still need two make some more tests, but if you can still give your early thought. Thanks all! |
Wow, ok! 😍 . I will give all of the Twig component-related PRs a review tomorrow! |
$compiler | ||
->write('$context,[') | ||
->write("'slot' => new ".ComponentSlot::class." (\$slot),\n") | ||
->write("'attributes' => new ".AttributeBag::class.'('); |
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 seems to conflict / run over the normal attributes
variable inside of a Twig component.
In general, I was thinking that instead of the default slot becoming a slot
variable and then named slots becoming their own local variables, WDYT about having a slots
variable?
<div>
{{ slots.content }}
<div {{ slots.footer.attributes }}>
{{ slots.footer }}
</div>
</div>
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.
Yes, the slots variable is a good idea, I push a commit to add it 😁. And for the attributes, I will keep as it currently is. And use the original ComponentAttributes class for slot as well or a similar interface.
@WebMamba could you do a rebase? I'm still thinking about all this stuff, but current opinion is:
I am another template!
<twig:FooBar>
{# the "this" variable would either be undefined, or refer to the component that this entire template is for, if we're already inside of a component template #}
{{ component.methodOnFooBar() }}
</twig:FooBar>
Rendering {{ firstName }} from host template.
<twig:FooBar>
The {{ firstName }} variable also exists here.
</twig:FooBar> If you choose to override blocks inside of a template, then the rules change, but this is for more advanced use-cases. Rendering {{ firstName }} from host template.
<twig:FooBar>
{% block header %}
Inside here, I would have access to whatever variables are inside of the `header` block inside `FooBar.html.twig` - that's how the current system works. The `this` variable is now `FooBar`
{% endblock %}
</twig:FooBar> I'm not sure how close we are to this yet... or if you or @sneakyvv will have some disagreements. |
Btw, about this PR, you test it a bit on the ux.symfony.com codebase for some realistic examples - that's where I spotted the I also noticed the, if I put some default content, it appears to fill in both the
Then in I noticed that either of these work:
In fact, if you try that exactly, you'll see it's printed twice. Are we, perhaps, still wrapping
I get:
I'm guessing my request to mix slots into the existing "embed" syntax is causing some complications. Let me know what you think - or if what I'm imagining is impossible / not feasible. Thanks! |
de2d297
to
c6312ae
Compare
Done 😁
I don't know... Let me try things out this week-end and I will give you an answer. 😁 No worries, about that, I just like to try thing before making any decision 😎
Yes, I made that on purpose. What do you think about it? Do you think we should only keep the default slot?
Humm no that is the bug in the ComponentLexer, it should add all the slot and content in the default block |
Hey @weaverryan! I simplify a lot this PR. So instead of adding a new tag, I used the component tag. I am focusing here on adding slot and anonymous components. |
Hey @WebMamba! Sorry for the delay - this is a complex issue to get right! I'm interested to play with this PR and see how it feels, especially in comparison with using a lot of blocks. I tried pulling this in on ux.symfony.com, but it bombs on the homepage:
Is that a bug? Expected? Also, to clarify the scope, does this PR still attempt to make the "default" content inside of an embedded component - e.g. Thanks! |
Is there an advantage to having this feature? |
I am closing this |
😢 it's still a pain to work with |
Hey all!
Here is a proposal to add a new twig_component tag and a slot tag, as mentioned in this issue: #844.
The idea is to deprecate the component tag, in favor of the twig_component tag introduce in this PR.
What's gonna change?
Using blocks inside components ends up with issues and some limitations. So here instead of using blocks, we use slots. Slot is a new concept that only TwigComponent gonna care about. But for the rest, all the features stay untouched.
So let's illustrate with some example:
As you can see everything you include in the twig_component tag can be retrieved in your component template by using the slot variable.
You can also use some custom slots by using the slot tag:
And the content inside the slot tag will be set into the footer variable.
This new approach came with a nice feature, the Anonymous component!!! 🎉
Let's imagine you have the following template:
And the Button component does not exist, so Twig will search for a template:
if one of these templates exists Twig render it like an anonymous component.
Of course the goal is not to use this syntaxt directly but to use it with the new HTML syntax:
and if you want to use custom slot
There is still some work to do:
But tell me what you think about it!
Thank you all! 😁