@@ -1555,8 +1555,8 @@ as currency:
1555
1555
1556
1556
.. _templates-twig-filter-attribute :
1557
1557
1558
- Create a class with a method that contains the filter logic, then add
1559
- the ``#[AsTwigFilter] `` attribute to define the name and options of
1558
+ Create a regular PHP class with a method that contains the filter logic. Then,
1559
+ add the ``#[AsTwigFilter] `` attribute to define the name and options of
1560
1560
the Twig filter::
1561
1561
1562
1562
// src/Twig/AppExtension.php
@@ -1602,13 +1602,15 @@ If you want to create a function instead of a filter, use the
1602
1602
1603
1603
.. versionadded :: 7.3
1604
1604
1605
- Support for the ``#[AsTwigFilter] ``, ``#[AsTwigFunction] `` and ``#[AsTwigTest] `` attributes was introduced in Symfony 7.3.
1606
- Previously, you had to extend the ``AbstractExtension `` class, and override the
1607
- ``getFilters() `` and ``getFunctions() `` methods.
1605
+ Support for the ``#[AsTwigFilter] ``, ``#[AsTwigFunction] `` and ``#[AsTwigTest] ``
1606
+ attributes was introduced in Symfony 7.3. Previously, you had to extend the
1607
+ ``AbstractExtension `` class, and override the ``getFilters() `` and ``getFunctions() ``
1608
+ methods.
1608
1609
1609
- When using autoconfiguration, the tag ``twig.attribute_extension `` is added automatically
1610
- when a Twig attribute is used on a method of a class. Otherwise, when autoconfiguration is not enabled,
1611
- it needs to be added in the service definition.
1610
+ If you're using the :ref: `default services.yaml configuration <service-container-services-load-example >`,
1611
+ the :ref: `service autoconfiguration <services-autoconfigure >` feature will enable
1612
+ this class as a Twig extension. Otherwise, you need to define a service manually
1613
+ and :doc: `tag it </service_container/tags >` with the ``twig.attribute_extension `` tag.
1612
1614
1613
1615
Register an Extension as a Service
1614
1616
..................................
@@ -1633,10 +1635,11 @@ this command to confirm that your new filter was successfully registered:
1633
1635
Creating Lazy-Loaded Twig Extensions
1634
1636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1635
1637
1636
- When using attributes to extend Twig, the services are initialized only when
1637
- the functions or filters are used to render the template. But in case you use the
1638
- classic approach by extending the ``AbstractExtension `` class, Twig initializes all the extensions before
1639
- rendering any template, even if the extension is not used in the template.
1638
+ When :ref: `using attributes to extend Twig <templates-twig-filter-attribute >`,
1639
+ the **Twig extensions are already lazy-loaded ** and you don't have to do anything
1640
+ else. However, if your Twig extensions follow the **legacy approach ** of extending
1641
+ the ``AbstractExtension `` class, Twig initializes all the extensions before
1642
+ rendering any template, even if they are not used.
1640
1643
1641
1644
If extensions don't define dependencies (i.e. if you don't inject services in
1642
1645
them) performance is not affected. However, if extensions define lots of complex
0 commit comments