Skip to content

[TwigBundle] Enable #[AsTwigFilter], #[AsTwigFunction] and #[AsTwigTest] #20879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions quick_tour/the_architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ that extends ``AbstractExtension``::
namespace App\Twig;

use App\GreetingGenerator;
use Twig\Attribute\AsTwigFilter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class GreetExtension extends AbstractExtension
{
Expand All @@ -175,13 +175,7 @@ that extends ``AbstractExtension``::
) {
}

public function getFilters(): array
{
return [
new TwigFilter('greet', [$this, 'greetUser']),
];
}

#[AsTwigFilter('greet')]
public function greetUser(string $name): string
{
$greeting = $this->greetingGenerator->getRandomGreeting();
Expand Down
2 changes: 2 additions & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Twig
~~~~

* :ref:`Template <templates-template-attribute>`
* :ref:`AsTwigFilter <templates-twig-filter-attribute>`
* :ref:`AsTwigFunction <templates-twig-function-attribute>`

Symfony UX
~~~~~~~~~~
Expand Down
35 changes: 16 additions & 19 deletions templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1553,23 +1553,19 @@ as currency:
{# pass in the 3 optional arguments #}
{{ product.price|price(2, ',', '.') }}

.. _templates-twig-filter-attribute:

Create a class that extends ``AbstractExtension`` and fill in the logic::

// src/Twig/AppExtension.php
namespace App\Twig;

use Twig\Attribute\AsTwigFilter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('price', [$this, 'formatPrice']),
];
}

#[AsTwigFilter('price')]
public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string
{
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
Expand All @@ -1579,24 +1575,20 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
}
}

If you want to create a function instead of a filter, define the
``getFunctions()`` method::
.. _templates-twig-function-attribute:

If you want to create a function instead of a filter, use the
``AsTwigFunction`` attribute::

// src/Twig/AppExtension.php
namespace App\Twig;

use Twig\Attribute\AsTwigFunction;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('area', [$this, 'calculateArea']),
];
}

#[AsTwigFunction('area')]
public function calculateArea(int $width, int $length): int
{
return $width * $length;
Expand All @@ -1608,6 +1600,11 @@ If you want to create a function instead of a filter, define the
Along with custom filters and functions, you can also register
`global variables`_.

.. versionadded:: 7.3

Support for the ``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` attributes was introduced in Symfony 7.3.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can add the #[AsTwigTest] to the list.

Previously, you had to use the ``getFilters()`` and ``getFunctions()`` methods.

Register an Extension as a Service
..................................

Expand All @@ -1634,7 +1631,7 @@ Creating Lazy-Loaded Twig Extensions
Including the code of the custom filters/functions in the Twig extension class
is the simplest way to create extensions. However, Twig must initialize all
extensions before rendering any template, even if the template doesn't use an
extension.
extension. Note that if you use attributes, this part is not needed.

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