Skip to content

Commit 7272da2

Browse files
fix comments made by GromNaN
1 parent 2d4e09a commit 7272da2

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

quick_tour/the_architecture.rst

Lines changed: 1 addition & 1 deletion
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 Twig attributes
194+
How does this work? Symfony notices that your class uses a Twig 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.

templates.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,9 @@ as currency:
15551555
15561556
.. _templates-twig-filter-attribute:
15571557

1558-
Create a class and fill in the logic::
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
1560+
the Twig filter::
15591561

15601562
// src/Twig/AppExtension.php
15611563
namespace App\Twig;
@@ -1577,7 +1579,7 @@ Create a class and fill in the logic::
15771579
.. _templates-twig-function-attribute:
15781580

15791581
If you want to create a function instead of a filter, use the
1580-
``AsTwigFunction`` attribute::
1582+
``#[AsTwigFunction]`` attribute::
15811583

15821584
// src/Twig/AppExtension.php
15831585
namespace App\Twig;
@@ -1601,7 +1603,8 @@ If you want to create a function instead of a filter, use the
16011603
.. versionadded:: 7.3
16021604

16031605
Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3.
1604-
Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods.
1606+
Previously, you had to extend the `AbstractExtension` class, and override the
1607+
``getFilters()`` and ``getFunctions()`` methods.
16051608

16061609
Register an Extension as a Service
16071610
..................................
@@ -1626,10 +1629,11 @@ this command to confirm that your new filter was successfully registered:
16261629
Creating Lazy-Loaded Twig Extensions
16271630
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16281631

1629-
Including the code of the custom filters/functions in the Twig extension class
1630-
is the simplest way to create extensions. However, Twig must initialize all
1631-
extensions before rendering any template, even if the template doesn't use an
1632-
extension. Note that if you use attributes, this part is not needed.
1632+
When using attributes to extend Twig, the services are initialized only when
1633+
the functions or filters are used to render the template. But in case you use the
1634+
classic approach by extending the ``AbstractExtension`` class, Twig initialize all extensions before
1635+
rendering rendering any template, even if the template doesn't use an
1636+
extension.
16331637

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

0 commit comments

Comments
 (0)