Skip to content

Commit 413367e

Browse files
committed
add AsTwig* attributes
1 parent f70f43d commit 413367e

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

quick_tour/the_architecture.rst

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,24 @@ Twig Extension & Autoconfiguration
159159
Thanks to Symfony's service handling, you can *extend* Symfony in many ways, like
160160
by creating an event subscriber or a security voter for complex authorization
161161
rules. Let's add a new filter to Twig called ``greet``. How? Create a class
162-
that extends ``AbstractExtension``::
162+
and use ``AsTwigFilter``::
163163

164164
// src/Twig/GreetExtension.php
165165
namespace App\Twig;
166166

167-
use App\GreetingGenerator;
168-
use Twig\Extension\AbstractExtension;
169-
use Twig\TwigFilter;
167+
use Twig\Attribute\AsTwigFilter;
170168

171-
class GreetExtension extends AbstractExtension
169+
class GreetExtension
172170
{
173171
public function __construct(
174172
private GreetingGenerator $greetingGenerator,
175173
) {
176174
}
177175

178-
public function getFilters(): array
179-
{
180-
return [
181-
new TwigFilter('greet', [$this, 'greetUser']),
182-
];
183-
}
184-
176+
#[AsTwigFilter('greet')]
185177
public function greetUser(string $name): string
186178
{
187-
$greeting = $this->greetingGenerator->getRandomGreeting();
179+
$greeting = $this->greetingGenerator->getRandomGreeting();
188180

189181
return "$greeting $name!";
190182
}

templates.rst

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,24 +1553,17 @@ as currency:
15531553
{# pass in the 3 optional arguments #}
15541554
{{ product.price|price(2, ',', '.') }}
15551555
1556-
Create a class that extends ``AbstractExtension`` and fill in the logic::
1556+
Create a class and fill in the logic::
15571557

15581558
// src/Twig/AppExtension.php
15591559
namespace App\Twig;
15601560

1561-
use Twig\Extension\AbstractExtension;
1562-
use Twig\TwigFilter;
1561+
use Twig\Attribute\AsTwigTest;
15631562

1564-
class AppExtension extends AbstractExtension
1563+
class AppExtension
15651564
{
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
15741567
{
15751568
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
15761569
$price = '$'.$price;
@@ -1579,30 +1572,45 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
15791572
}
15801573
}
15811574

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::
15841577

15851578
// src/Twig/AppExtension.php
15861579
namespace App\Twig;
15871580

1588-
use Twig\Extension\AbstractExtension;
1589-
use Twig\TwigFunction;
1581+
use Twig\Attribute\AsTwigFunction;
15901582

1591-
class AppExtension extends AbstractExtension
1583+
class AppExtension
15921584
{
1593-
public function getFunctions(): array
1585+
#[AsTwigFunction('area')]
1586+
public static function calculateArea(int $width, int $length): int
15941587
{
1595-
return [
1596-
new TwigFunction('area', [$this, 'calculateArea']),
1597-
];
1588+
return $width * $length;
15981589
}
1590+
}
1591+
1592+
You can also create a test using the ``AsTwigTest`` attribute::
15991593

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
16011603
{
1602-
return $width * $length;
1604+
return $number % 2 === 0;
16031605
}
16041606
}
16051607

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+
16061614
.. tip::
16071615

16081616
Along with custom filters and functions, you can also register
@@ -1616,7 +1624,7 @@ using the :ref:`default services.yaml configuration <service-container-services-
16161624
you're done! Symfony will automatically know about your new service and add the tag.
16171625

16181626
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 :
16201628

16211629
.. code-block:: terminal
16221630
@@ -1636,6 +1644,11 @@ is the simplest way to create extensions. However, Twig must initialize all
16361644
extensions before rendering any template, even if the template doesn't use an
16371645
extension.
16381646

1647+
.. note::
1648+
1649+
When using the ``AsTwigFilter``, ``AsTwigFunction`` or ``AsTwigTest`` attributes,
1650+
lazy loading is performed automatically..
1651+
16391652
If extensions don't define dependencies (i.e. if you don't inject services in
16401653
them) performance is not affected. However, if extensions define lots of complex
16411654
dependencies (e.g. those making database connections), the performance loss can

0 commit comments

Comments
 (0)