-
-
Notifications
You must be signed in to change notification settings - Fork 364
[TwigComponent] Add 'require ** as **' to alias components #1209
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?
[TwigComponent] Add 'require ** as **' to alias components #1209
Conversation
Sadly still have to prefix with |
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.
Whoooo thanks @maelanleborgne to get into this 🧡
@@ -167,7 +167,7 @@ public function preLexComponents(string $input): string | |||
throw new SyntaxError(sprintf('Expected closing tag "</twig:%s>" not found.', $lastComponent), $this->line); | |||
} | |||
|
|||
return $output; | |||
return $this->processRequires($output); |
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 do you think about dispatching an event PostTwigPrelexing
and putting the processRequires function's content in a Subscriber?
@norkunas what do you propose then? I feel like people like this syntax |
See my issue :) |
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.
@maelanleborgne I was thinking again about your PR. And I don't think to use regex is not the good path here. You have to replicate some logic like verbatim or comment blocks... And if for exemple someone made a typo like so {% require Foo as Bar }
we not gonna deliver a proper errors. I think we should do the logic from the TwigPrelexer directly. When the TwigPrelexer find {% require
then he look for componentName and his alias, store this in property. Then when the TwigPrelexer is opening a component he looks via his property if an alias doesn't exist for this componentName. What you think ?
For me it's too much of the prefix when using many components.. |
I love the idea... but i'd like to solve our problems with the two lexers first if you're OK to wait a couple of days :| We may stop resolving the component names at prelex time, so your implementation could be much easier ... or almost impossible depending on what we want to do :) Currently with your implementation, i fear the following use case would break, no ? {{ component(a_var) }} {% verbatim %}
{{ component('foo') }}
{% endverbatim %} {% embed %}
{% block 'inside' %}
{{ component('foo') }}
{% endblock %}
{{ component('foo') }}
{% endembed %} It seems to me that we would be safer with a NodeVisitor (like trans_domain for example) |
Another complex case
|
Another case {# templates/_blocks.html.twig #}
{% block sidebar_top %}
<twig:Crocodile:BigCroco />
{% endblock %}
{% block sidebar_bottom %}
<twig:Crocodile:LittleCroco />
{% endblock %} {# templates/homepage.html.twig #}
{% require Fake:Crocodile as Crocodile %}
{% require Plastic:BigCroco as Crocodile:BigCroco %}
{% use '_blocks.html.twig' %}
<twig:Crocodile:LittleCroco />
{% bloc sidebar_top %}
<twig:Crocodile:BigCroco />
{{ parent() }}
{% endblock %}
{% bloc sidebar_bottom %}
<twig:Crocodile:LittleCroco />
{% endblock %} (side note: this one is another reminder we should not pre-resolve names before the render @weaverryan ... anything done before the render is a risk some Twig feature stop working) |
Nice feature! I hope it will be merged soon! |
Ah yes, this kind of functionality is interesting for organizing components in files. |
You can create a |
The idea is to structure the
|
Why not the following structure?
It would allow you to use components like this: <twig:Sidebar>
<twig:Sidebar:Header></twig:Sidebar:Header>
<twig:Sidebar:Body></twig:Sidebar:Body>
<twig:Sidebar:Item></twig:Sidebar:Item>
<twig:Sidebar:Label></twig:Sidebar:Label>
</twig:Sidebar> |
I understand your logic and that works :). The idea I propose is to group the related components within a single directory. |
As suggested in #1108 and #801, this PR adds a new step in
TwigPreLexer
to replace components aliases by there full name.This will be useful when reusing many times the same components in your template, especially when making extensive use of components : having a well organized component folder will lead to longer component names.
What do you think ?
TODO: