New in Symfony 5.3: Twig Serialize Filter

Contributed by Jesse Rushlow in #40323.

In Symfony applications there are several methods of passing data from the backend to the frontend. Some applications make Ajax requests whenever they need data and others prefer to pass it in Twig templates as HTML attributes or JavaScript variable values. In Symfony 5.3 we’ve added a simpler way of doing this: the new serialize Twig filter. This filter accepts any data that can be serialized by the Serializer component and returns a serialized string in the specified format. In the following example, the Twig template uses the stimulus_controller() function from the Symfony Stimulus Bridge. If the template is passed a variable called product, you can now serialize its contents to use it in the Stimulus function call: 1 2 3{{ stimulus_controller('product-show', { product: product|serialize('json', { groups: 'product:read'}) }) }}

This would render something like this: 1 2 3<div data-controller="product-show" data-product-show-product-value="{&quot;id&quot;:5,...}"> <div>

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/ul7ItnkAHbI/new-in-symfony-5-3-twig-serialize-filter

Établi 4y | 17 mai 2021, 08:20:12


Connectez-vous pour ajouter un commentaire

Autres messages de ce groupe

SymfonyCon Amsterdam 2025:   Unconference Track at SymfonyCon Amsterdam 2025

🎤 Take the stage at SymfonyCon Amsterdam 2025, on your own terms!

The Unconference track is back and more dynamic than ever!

This unique, participant-driven format invites attendees to shape

12 août 2025, 12:40:20 | Symfony
A Week of Symfony #971 (August 4–10, 2025)

This week, Symfony completed the migration to PHPUnit 12 in the 7.4 branch, which required many changes during the past weeks, such as replacing annotations with attributes. In addition, we updated th

10 août 2025, 09:40:09 | Symfony
SymfonyCon Amsterdam 2025:  Join the Symfony Hackathon:  Collaborate, Contribute, Create

🧑‍💻HACKDAY IS COMING!

Get ready to code, collaborate, and contribute, Symfony Hackday is back!

Join us in Amsterdam on Saturday, November 29th, for a hands-on hackathon designed to bring the

6 août 2025, 10:40:04 | Symfony
A Week of Symfony #970 (July 28 – August 3, 2025)

This week, Symfony released the maintenance versions 6.4.24, 7.2.9, and 7.3.2. Meanwhile, we began deprecating the XML configuration format in some components, enhanced the YAML configuration format t

3 août 2025, 08:30:03 | Symfony
Symfony 6.4.24 released

Symfony 6.4.24 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in you

31 juil. 2025, 13:10:32 | Symfony
Symfony 7.2.9 released

Symfony 7.2.9 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in your

31 juil. 2025, 13:10:31 | Symfony
Symfony 7.3.2 released

Symfony 7.3.2 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in your

31 juil. 2025, 13:10:30 | Symfony