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

Létrehozva 4y | 2021. máj. 17. 8:20:12


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

2025. febr. 24. 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

2025. febr. 24. 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

2025. febr. 23. 10:10:09 | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

2025. febr. 21. 9:20:12 | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

2025. febr. 20. 10:10:13 | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

2025. febr. 19. 8:40:05 | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

2025. febr. 18. 14:10:24 | Symfony