-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[TwigBundle] Enable #[AsTwigFilter], #[AsTwigFunction] and #[AsTwigTest]
#20879
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: 7.3
Are you sure you want to change the base?
Conversation
I have no clue why the CI fails to build the docs, I can build it properly on my end 😞 |
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.
I've added a few comments. I find it hard to juggle the old and new ways of doing things in the same doc without making it too complex to understand; but it looks fine to me the way it is.
|
||
How does this work? Symfony notices that your class extends ``AbstractExtension`` | ||
How does this work? Symfony notices that your class uses Twig 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.
How does this work? Symfony notices that your class uses Twig attributes | |
How does this work? Symfony notices that your class uses a Twig attribute |
.. _templates-twig-function-attribute: | ||
|
||
If you want to create a function instead of a filter, use the | ||
``AsTwigFunction`` attribute:: |
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.
``AsTwigFunction`` attribute:: | |
``#[AsTwigFunction]`` attribute:: |
|
||
Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3. | ||
Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods. | ||
|
||
Register an Extension as a Service | ||
.................................. | ||
|
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.
Just below, the tag to use when the class uses attributes is twig.attribute_extension
. But this tag is added automatically by autoconfiguration.
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.
So do we mention that this tag is automatically added by autoconfiguration? Or not? I'm a bit scared it would just make the section a bit hard to read :/
Create a class that extends ``AbstractExtension`` and fill in the logic:: | ||
.. _templates-twig-filter-attribute: | ||
|
||
Create a class and fill in the logic:: |
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.
Create a class and fill in the logic:: | |
Create a class with a method that contains the filter logic, then add | |
the `#[AsTwigFilter]` attribute to define the name and options of | |
the Twig filter:: |
.. versionadded:: 7.3 | ||
|
||
Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3. | ||
Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods. |
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.
Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods. | |
Previously, you had to extend the `AbstractExtension` class, and override the | |
``getFilters()`` and ``getFunctions()`` methods. |
Including the code of the custom filters/functions in the Twig extension class | ||
is the simplest way to create extensions. However, Twig must initialize all | ||
extensions before rendering any template, even if the template doesn't use an | ||
extension. | ||
extension. Note that if you use attributes, this part is not needed. |
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 could instead begin by mentioning the attributes in the very first sentence.
When using attributes to extend Twig, the services are initialized only when the functions or filters are used to render the template. But in case you use the classic approach by extending the
AbstractExtension
class, Twig initialize all extensions before rendering...
Thanks for the comment GromNan, it's very appreciated especially since I'm still new to the doc repository. I'm slowly starting to get the hang of how to write it 🎉. I'll apply your suggestions tomorrow 😄 |
fix #20828
I did not listed the
AsTwigTest
attribute in the attributes list as no doc make mention of use case of this attribute. Do I had it anyway without any link to it (or a link to the twig documentation)?Also I hope my wording is correct, feel free to ask me any adjustment 😄
I followed the plan of @javiereguiluz