Skip to content

Commit b103906

Browse files
fix 20828 by replacing methods with attributes
1 parent 344d8ba commit b103906

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

quick_tour/the_architecture.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ that extends ``AbstractExtension``::
166166

167167
use App\GreetingGenerator;
168168
use Twig\Extension\AbstractExtension;
169-
use Twig\TwigFilter;
169+
use Twig\Attribute\AsTwigFilter;
170170

171171
class GreetExtension extends AbstractExtension
172172
{
@@ -175,13 +175,7 @@ that extends ``AbstractExtension``::
175175
) {
176176
}
177177

178-
public function getFilters(): array
179-
{
180-
return [
181-
new TwigFilter('greet', [$this, 'greetUser']),
182-
];
183-
}
184-
178+
#[AsTwigFilter('greet')]
185179
public function greetUser(string $name): string
186180
{
187181
$greeting = $this->greetingGenerator->getRandomGreeting();

reference/attributes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Twig
123123
~~~~
124124

125125
* :ref:`Template <templates-template-attribute>`
126+
* :ref:`AsTwigFilter <templates-twig-filter-attribute>`
127+
* :ref:`AsTwigFunction <templates-twig-function-attribute>`
126128

127129
Symfony UX
128130
~~~~~~~~~~

templates.rst

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,23 +1553,19 @@ as currency:
15531553
{# pass in the 3 optional arguments #}
15541554
{{ product.price|price(2, ',', '.') }}
15551555
1556+
.. _templates-twig-filter-attribute:
1557+
15561558
Create a class that extends ``AbstractExtension`` and fill in the logic::
15571559

15581560
// src/Twig/AppExtension.php
15591561
namespace App\Twig;
15601562

15611563
use Twig\Extension\AbstractExtension;
1562-
use Twig\TwigFilter;
1564+
use Twig\Attribute\AsTwigFilter;
15631565

15641566
class AppExtension extends AbstractExtension
15651567
{
1566-
public function getFilters(): array
1567-
{
1568-
return [
1569-
new TwigFilter('price', [$this, 'formatPrice']),
1570-
];
1571-
}
1572-
1568+
#[AsTwigFilter('price')]
15731569
public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string
15741570
{
15751571
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
@@ -1579,24 +1575,20 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
15791575
}
15801576
}
15811577

1582-
If you want to create a function instead of a filter, define the
1583-
``getFunctions()`` method::
1578+
.. _templates-twig-function-attribute:
1579+
1580+
If you want to create a function instead of a filter, use the
1581+
``AsTwigFunction`` attribute::
15841582

15851583
// src/Twig/AppExtension.php
15861584
namespace App\Twig;
15871585

15881586
use Twig\Extension\AbstractExtension;
1589-
use Twig\TwigFunction;
1587+
use Twig\Attribute\AsTwigFunction;
15901588

15911589
class AppExtension extends AbstractExtension
15921590
{
1593-
public function getFunctions(): array
1594-
{
1595-
return [
1596-
new TwigFunction('area', [$this, 'calculateArea']),
1597-
];
1598-
}
1599-
1591+
#[AsTwigFunction('area')]
16001592
public function calculateArea(int $width, int $length): int
16011593
{
16021594
return $width * $length;
@@ -1608,6 +1600,11 @@ If you want to create a function instead of a filter, define the
16081600
Along with custom filters and functions, you can also register
16091601
`global variables`_.
16101602

1603+
.. versionadded:: 7.3
1604+
1605+
Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3.
1606+
Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods.
1607+
16111608
Register an Extension as a Service
16121609
..................................
16131610

@@ -1634,7 +1631,7 @@ Creating Lazy-Loaded Twig Extensions
16341631
Including the code of the custom filters/functions in the Twig extension class
16351632
is the simplest way to create extensions. However, Twig must initialize all
16361633
extensions before rendering any template, even if the template doesn't use an
1637-
extension.
1634+
extension. Note that if you use attributes, this part is not needed.
16381635

16391636
If extensions don't define dependencies (i.e. if you don't inject services in
16401637
them) performance is not affected. However, if extensions define lots of complex

0 commit comments

Comments
 (0)