@@ -1553,24 +1553,17 @@ as currency:
1553
1553
{# pass in the 3 optional arguments #}
1554
1554
{{ product.price|price(2, ',', '.') }}
1555
1555
1556
- Create a class that extends `` AbstractExtension `` and fill in the logic::
1556
+ Create a class and fill in the logic::
1557
1557
1558
1558
// src/Twig/AppExtension.php
1559
1559
namespace App\Twig;
1560
1560
1561
- use Twig\Extension\AbstractExtension;
1562
- use Twig\TwigFilter;
1561
+ use Twig\Attribute\AsTwigTest;
1563
1562
1564
- class AppExtension extends AbstractExtension
1563
+ class AppExtension
1565
1564
{
1566
- public function getFilters(): array
1567
- {
1568
- return [
1569
- new TwigFilter('price', [$this, 'formatPrice']),
1570
- ];
1571
- }
1572
-
1573
- public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string
1565
+ #[AsTwigFilter('price')]
1566
+ public static function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string
1574
1567
{
1575
1568
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
1576
1569
$price = '$'.$price;
@@ -1579,30 +1572,45 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
1579
1572
}
1580
1573
}
1581
1574
1582
- If you want to create a function instead of a filter, define the
1583
- ``getFunctions() `` method ::
1575
+ If you want to create a function instead of a filter, use the
1576
+ ``AsTwigFunction `` attribute ::
1584
1577
1585
1578
// src/Twig/AppExtension.php
1586
1579
namespace App\Twig;
1587
1580
1588
- use Twig\Extension\AbstractExtension;
1589
- use Twig\TwigFunction;
1581
+ use Twig\Attribute\AsTwigFunction;
1590
1582
1591
- class AppExtension extends AbstractExtension
1583
+ class AppExtension
1592
1584
{
1593
- public function getFunctions(): array
1585
+ #[AsTwigFunction('area')]
1586
+ public static function calculateArea(int $width, int $length): int
1594
1587
{
1595
- return [
1596
- new TwigFunction('area', [$this, 'calculateArea']),
1597
- ];
1588
+ return $width * $length;
1598
1589
}
1590
+ }
1591
+
1592
+ You can also create a test using the ``AsTwigTest `` attribute::
1599
1593
1600
- public function calculateArea(int $width, int $length): int
1594
+ // src/Twig/AppExtension.php
1595
+ namespace App\Twig;
1596
+
1597
+ use Twig\Attribute\AsTwigTest;
1598
+
1599
+ class AppExtension
1600
+ {
1601
+ #[AsTwigTest('even')]
1602
+ public static function isEven(int $number): bool
1601
1603
{
1602
- return $width * $length ;
1604
+ return $number % 2 === 0 ;
1603
1605
}
1604
1606
}
1605
1607
1608
+ .. versionadded :: 7.3
1609
+
1610
+ The ``AsTwigFilter ``, ``AsTwigFunction `` and ``AsTwigTest `` attributes were introduced in Symfony 7.3.
1611
+ Before Symfony 7.3, you had to implement the ``getFilters() ``, ``getFunctions() `` methods.
1612
+ See the `Twig Extensions `_ for more information.
1613
+
1606
1614
.. tip ::
1607
1615
1608
1616
Along with custom filters and functions, you can also register
@@ -1616,7 +1624,7 @@ using the :ref:`default services.yaml configuration <service-container-services-
1616
1624
you're done! Symfony will automatically know about your new service and add the tag.
1617
1625
1618
1626
You can now start using your filter in any Twig template. Optionally, execute
1619
- this command to confirm that your new filter was successfully registered :
1627
+ this command to confirm that your new filter was successfully registeredstatic :
1620
1628
1621
1629
.. code-block :: terminal
1622
1630
@@ -1636,6 +1644,11 @@ is the simplest way to create extensions. However, Twig must initialize all
1636
1644
extensions before rendering any template, even if the template doesn't use an
1637
1645
extension.
1638
1646
1647
+ .. note ::
1648
+
1649
+ When using the ``AsTwigFilter ``, ``AsTwigFunction `` or ``AsTwigTest `` attributes,
1650
+ lazy loading is performed automatically..
1651
+
1639
1652
If extensions don't define dependencies (i.e. if you don't inject services in
1640
1653
them) performance is not affected. However, if extensions define lots of complex
1641
1654
dependencies (e.g. those making database connections), the performance loss can
0 commit comments