Skip to content

Commit a657538

Browse files
committed
Tweaks
1 parent 7199104 commit a657538

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

quick_tour/the_architecture.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ After creating just *one* file, you can use this immediately:
191191
{# Will print something like "Hey Symfony!" #}
192192
<h1>{{ name|greet }}</h1>
193193

194-
How does this work? Symfony notices that your class uses a Twig attribute
194+
How does this work? Symfony notices that your class uses the ``#[AsTwigFilter]`` attribute
195195
and so *automatically* registers it as a Twig extension. This is called autoconfiguration,
196196
and it works for *many* many things. Create a class and then extend a base class
197197
(or implement an interface). Symfony takes care of the rest.

reference/attributes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Twig
125125
* :ref:`Template <templates-template-attribute>`
126126
* :ref:`AsTwigFilter <templates-twig-filter-attribute>`
127127
* :ref:`AsTwigFunction <templates-twig-function-attribute>`
128+
* ``AsTwigTest``
128129

129130
Symfony UX
130131
~~~~~~~~~~

templates.rst

+15-12
Original file line numberDiff line numberDiff line change
@@ -1555,8 +1555,8 @@ as currency:
15551555
15561556
.. _templates-twig-filter-attribute:
15571557

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
15601560
the Twig filter::
15611561

15621562
// src/Twig/AppExtension.php
@@ -1602,13 +1602,15 @@ If you want to create a function instead of a filter, use the
16021602

16031603
.. versionadded:: 7.3
16041604

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.
16081609

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.
16121614

16131615
Register an Extension as a Service
16141616
..................................
@@ -1633,10 +1635,11 @@ this command to confirm that your new filter was successfully registered:
16331635
Creating Lazy-Loaded Twig Extensions
16341636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16351637

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.
16401643

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

0 commit comments

Comments
 (0)