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

Utworzony 4y | 17 maj 2021, 08:20:12


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

22 kwi 2025, 09:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

20 kwi 2025, 08:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

16 kwi 2025, 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

15 kwi 2025, 14:50:24 | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

13 kwi 2025, 09:40:02 | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

10 kwi 2025, 16:40:34 | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

8 kwi 2025, 09:10:13 | Symfony