diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 270c9c678c8..e4991845096 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -558,6 +558,29 @@ project's root directory: If the given file path is out of the project directory, a ``null`` value will be returned. +serialize +~~~~~~~~~ + +.. code-block:: twig + + {{ object|serialize(format = 'json', context = []) }} + +``object`` + **type**: ``mixed`` + +``format`` *(optional)* + **type**: ``string`` + +``context`` *(optional)* + **type**: ``array`` + +.. versionadded:: 5.3 + + The ``serialize`` filter was introduced in Symfony 5.3. + +Accepts any data that can be serialized by the :doc:`Serializer component ` +and returns a serialized string in the specified ``format``. + .. _reference-twig-tags: Tags diff --git a/serializer.rst b/serializer.rst index b4dd7e03d52..7d526ae46bc 100644 --- a/serializer.rst +++ b/serializer.rst @@ -41,6 +41,19 @@ you need it or it can be used in a controller:: } } +Or you can use the ``serialize`` Twig filter in a template: + +.. code-block:: twig + + {{ object|serialize(format = 'json') }} + +See the :doc:`twig reference ` for +more information. + +.. versionadded:: 5.3 + + A ``serialize`` filter was introduced in Symfony 5.3 that uses the Serializer component. + Adding Normalizers and Encoders -------------------------------