Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -159,29 +159,22 @@ Twig Extension & Autoconfiguration | ||
Thanks to Symfony's service handling, you can *extend* Symfony in many ways, like | ||
by creating an event subscriber or a security voter for complex authorization | ||
rules. Let's add a new filter to Twig called ``greet``. How? Create a class | ||
with your logic:: | ||
// src/Twig/GreetExtension.php | ||
namespace App\Twig; | ||
use App\GreetingGenerator; | ||
use Twig\Attribute\AsTwigFilter; | ||
class GreetExtension | ||
{ | ||
public function __construct( | ||
private GreetingGenerator $greetingGenerator, | ||
) { | ||
} | ||
#[AsTwigFilter('greet')] | ||
public function greetUser(string $name): string | ||
{ | ||
$greeting = $this->greetingGenerator->getRandomGreeting(); | ||
@@ -197,8 +190,8 @@ After creating just *one* file, you can use this immediately: | ||
{# templates/default/index.html.twig #} | ||
{# Will print something like "Hey Symfony!" #} | ||
<h1>{{ name|greet }}</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. the added spaces should be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Did that while merging. Thanks. | ||
How does this work? Symfony notices that your classuses a Twig attribute | ||
and so *automatically* registers it as a Twig extension. This is called autoconfiguration, | ||
and it works for *many* many things. Create a class and then extend a base class | ||
(or implement an interface). Symfony takes care of the rest. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -123,6 +123,8 @@ Twig | ||
~~~~ | ||
* :ref:`Template <templates-template-attribute>` | ||
* :ref:`AsTwigFilter <templates-twig-filter-attribute>` | ||
* :ref:`AsTwigFunction <templates-twig-function-attribute>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What about also adding something for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I added | ||
Symfony UX | ||
~~~~~~~~~~ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1553,23 +1553,20 @@ as currency: | ||
{# pass in the 3 optional arguments #} | ||
{{ product.price|price(2, ',', '.') }} | ||
.. _templates-twig-filter-attribute: | ||
Create a class with a method that contains the filter logic, then add | ||
the ``#[AsTwigFilter]`` attribute to define the name and options of | ||
the Twig filter:: | ||
// src/Twig/AppExtension.php | ||
namespace App\Twig; | ||
use Twig\Attribute\AsTwigFilter; | ||
class AppExtension | ||
{ | ||
#[AsTwigFilter('price')] | ||
public function formatPrice(float $number, int $decimals = 0, string $decPoint = '.', string $thousandsSep = ','): string | ||
{ | ||
$price = number_format($number, $decimals, $decPoint, $thousandsSep); | ||
@@ -1579,24 +1576,19 @@ Create a class that extends ``AbstractExtension`` and fill in the logic:: | ||
} | ||
} | ||
.. _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; | ||
class AppExtension | ||
{ | ||
#[AsTwigFunction('area')] | ||
public function calculateArea(int $width, int $length): int | ||
{ | ||
return $width * $length; | ||
@@ -1608,6 +1600,16 @@ 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]``, ``#[AsTwigFunction]`` and ``#[AsTwigTest]`` attributes was introduced in Symfony 7.3. | ||
Previously, you had to extend the ``AbstractExtension`` class, and override the | ||
``getFilters()`` and ``getFunctions()`` methods. | ||
When using autoconfiguration, the tag ``twig.attribute_extension`` is added automatically | ||
when a Twig attribute is used on a method of a class. Otherwise, when autoconfiguration is not enabled, | ||
it needs to be added in the service definition. | ||
Register an Extension as a Service | ||
.................................. | ||
RisingSunLight42 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
@@ -1631,10 +1633,10 @@ this command to confirm that your new filter was successfully registered: | ||
Creating Lazy-Loaded Twig Extensions | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
When using attributes to extend Twig, the services are initialized only when | ||
thefunctions or filters are usedtorender the template. But in case you use the | ||
classic approach by extending the ``AbstractExtension`` class, Twig initializes all the extensions before | ||
rendering any template, even if theextension is not used in the template. | ||
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 | ||