@@ -1553,23 +1553,19 @@ as currency:
1553
1553
{# pass in the 3 optional arguments #}
1554
1554
{{ product.price|price(2, ',', '.') }}
1555
1555
1556
+ .. _templates-twig-filter-attribute :
1557
+
1556
1558
Create a class that extends ``AbstractExtension `` and fill in the logic::
1557
1559
1558
1560
// src/Twig/AppExtension.php
1559
1561
namespace App\Twig;
1560
1562
1561
1563
use Twig\Extension\AbstractExtension;
1562
- use Twig\TwigFilter ;
1564
+ use Twig\Attribute\AsTwigFilter ;
1563
1565
1564
1566
class AppExtension extends AbstractExtension
1565
1567
{
1566
- public function getFilters(): array
1567
- {
1568
- return [
1569
- new TwigFilter('price', [$this, 'formatPrice']),
1570
- ];
1571
- }
1572
-
1568
+ #[AsTwigFilter('price')]
1573
1569
public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string
1574
1570
{
1575
1571
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
@@ -1579,24 +1575,20 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
1579
1575
}
1580
1576
}
1581
1577
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::
1584
1582
1585
1583
// src/Twig/AppExtension.php
1586
1584
namespace App\Twig;
1587
1585
1588
1586
use Twig\Extension\AbstractExtension;
1589
- use Twig\TwigFunction ;
1587
+ use Twig\Attribute\AsTwigFunction ;
1590
1588
1591
1589
class AppExtension extends AbstractExtension
1592
1590
{
1593
- public function getFunctions(): array
1594
- {
1595
- return [
1596
- new TwigFunction('area', [$this, 'calculateArea']),
1597
- ];
1598
- }
1599
-
1591
+ #[AsTwigFunction('area')]
1600
1592
public function calculateArea(int $width, int $length): int
1601
1593
{
1602
1594
return $width * $length;
@@ -1608,6 +1600,11 @@ If you want to create a function instead of a filter, define the
1608
1600
Along with custom filters and functions, you can also register
1609
1601
`global variables `_.
1610
1602
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
+
1611
1608
Register an Extension as a Service
1612
1609
..................................
1613
1610
@@ -1634,7 +1631,7 @@ Creating Lazy-Loaded Twig Extensions
1634
1631
Including the code of the custom filters/functions in the Twig extension class
1635
1632
is the simplest way to create extensions. However, Twig must initialize all
1636
1633
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.
1638
1635
1639
1636
If extensions don't define dependencies (i.e. if you don't inject services in
1640
1637
them) performance is not affected. However, if extensions define lots of complex
0 commit comments