Edit on GitHub
Jump to docs navigation

Twig Components / Methods / serialize

Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?

serialize(format = "json", context = []) is a Twig filter to accept 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:

{{ stimulus_controller('product-show', {
    product: product|serialize('json', { groups: 'product:read'})
}) }}

This would render something like this:

<div data-controller="product-show"
     data-product-show-product-value="{&quot;id&quot;:5,...}">
<div>

Source: Twig



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.