-
-
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
Open
RisingSunLight42
wants to merge
5
commits into
symfony:7.3
Choose a base branch
from
RisingSunLight42:fix_20828
base: 7.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b103906
fix 20828 by replacing methods with attributes
RisingSunLight42 7952544
fix ordering in use statements
RisingSunLight42 2d4e09a
remove AbstractExtension extends
RisingSunLight42 7272da2
fix comments made by GromNaN
RisingSunLight42 b6e4367
resolve last conversation and fix CI
RisingSunLight42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1553,23 +1553,20 @@ as currency: | |||||||||||||||||||
{# pass in the 3 optional arguments #} | ||||||||||||||||||||
{{ product.price|price(2, ',', '.') }} | ||||||||||||||||||||
|
||||||||||||||||||||
Create a class that extends ``AbstractExtension`` and fill in the logic:: | ||||||||||||||||||||
.. _templates-twig-filter-attribute: | ||||||||||||||||||||
|
||||||||||||||||||||
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:: | ||||||||||||||||||||
|
||||||||||||||||||||
// src/Twig/AppExtension.php | ||||||||||||||||||||
namespace App\Twig; | ||||||||||||||||||||
|
||||||||||||||||||||
use Twig\Extension\AbstractExtension; | ||||||||||||||||||||
use Twig\TwigFilter; | ||||||||||||||||||||
use Twig\Attribute\AsTwigFilter; | ||||||||||||||||||||
|
||||||||||||||||||||
class AppExtension extends AbstractExtension | ||||||||||||||||||||
class AppExtension | ||||||||||||||||||||
{ | ||||||||||||||||||||
public function getFilters(): array | ||||||||||||||||||||
{ | ||||||||||||||||||||
return [ | ||||||||||||||||||||
new TwigFilter('price', [$this, 'formatPrice']), | ||||||||||||||||||||
]; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
#[AsTwigFilter('price')] | ||||||||||||||||||||
public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string | ||||||||||||||||||||
{ | ||||||||||||||||||||
$price = number_format($number, $decimals, $decPoint, $thousandsSep); | ||||||||||||||||||||
|
@@ -1579,24 +1576,19 @@ Create a class that extends ``AbstractExtension`` and fill in the logic:: | |||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
If you want to create a function instead of a filter, define the | ||||||||||||||||||||
``getFunctions()`` method:: | ||||||||||||||||||||
.. _templates-twig-function-attribute: | ||||||||||||||||||||
|
||||||||||||||||||||
If you want to create a function instead of a filter, use the | ||||||||||||||||||||
``#[AsTwigFunction]`` attribute:: | ||||||||||||||||||||
|
||||||||||||||||||||
// src/Twig/AppExtension.php | ||||||||||||||||||||
namespace App\Twig; | ||||||||||||||||||||
|
||||||||||||||||||||
use Twig\Extension\AbstractExtension; | ||||||||||||||||||||
use Twig\TwigFunction; | ||||||||||||||||||||
use Twig\Attribute\AsTwigFunction; | ||||||||||||||||||||
|
||||||||||||||||||||
class AppExtension extends AbstractExtension | ||||||||||||||||||||
class AppExtension | ||||||||||||||||||||
{ | ||||||||||||||||||||
public function getFunctions(): array | ||||||||||||||||||||
{ | ||||||||||||||||||||
return [ | ||||||||||||||||||||
new TwigFunction('area', [$this, 'calculateArea']), | ||||||||||||||||||||
]; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
#[AsTwigFunction('area')] | ||||||||||||||||||||
public function calculateArea(int $width, int $length): int | ||||||||||||||||||||
{ | ||||||||||||||||||||
return $width * $length; | ||||||||||||||||||||
|
@@ -1608,6 +1600,16 @@ If you want to create a function instead of a filter, define the | |||||||||||||||||||
Along with custom filters and functions, you can also register | ||||||||||||||||||||
`global variables`_. | ||||||||||||||||||||
|
||||||||||||||||||||
.. versionadded:: 7.3 | ||||||||||||||||||||
|
||||||||||||||||||||
Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3. | ||||||||||||||||||||
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. Here you can add the |
||||||||||||||||||||
Previously, you had to extend the ``AbstractExtension`` class, and override the | ||||||||||||||||||||
``getFilters()`` and ``getFunctions()`` methods. | ||||||||||||||||||||
|
||||||||||||||||||||
When using autoconfiguration, the tag ``twig.attribute_extension`` is added automatically | ||||||||||||||||||||
when a Twig attribute is used on a method of a class. Otherwise, when autoconfiguration is not enabled, | ||||||||||||||||||||
it needs to be added in the service definition. | ||||||||||||||||||||
|
||||||||||||||||||||
Register an Extension as a Service | ||||||||||||||||||||
.................................. | ||||||||||||||||||||
|
||||||||||||||||||||
RisingSunLight42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
@@ -1631,9 +1633,10 @@ this command to confirm that your new filter was successfully registered: | |||||||||||||||||||
Creating Lazy-Loaded Twig Extensions | ||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||
|
||||||||||||||||||||
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 | ||||||||||||||||||||
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 rendering any template, even if the template doesn't use an | ||||||||||||||||||||
extension. | ||||||||||||||||||||
Comment on lines
+1636
to
1640
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.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
If extensions don't define dependencies (i.e. if you don't inject services in | ||||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We avoid subjective words like "simple". "Plain" or nothing would be more adapted.