diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 1c20264352c..004d8595a71 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -352,6 +352,42 @@ t Creates a ``Translatable`` object that can be passed to the :ref:`trans filter `. +.. configuration-block:: + + .. code-block:: yaml + + # translations/blog.en.yaml + message: Hello %name% + + .. code-block:: xml + + + + + + + + message + Hello %name% + + + + + + .. code-block:: php + + // translations/blog.en.php + return [ + 'message' => "Hello %name%", + ]; + +Using the filter will be rendered as: + +.. code-block:: twig + + {{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }} + {# output: Hello John #} + Form Related Functions ~~~~~~~~~~~~~~~~~~~~~~ @@ -429,6 +465,42 @@ trans Translates the text into the current language. More information in :ref:`Translation Filters `. +.. configuration-block:: + + .. code-block:: yaml + + # translations/messages.en.yaml + message: Hello %name% + + .. code-block:: xml + + + + + + + + message + Hello %name% + + + + + + .. code-block:: php + + // translations/messages.en.php + return [ + 'message' => "Hello %name%", + ]; + +Using the filter will be rendered as: + +.. code-block:: twig + + {{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }} + {# output: Hello John #} + yaml_encode ~~~~~~~~~~~ @@ -476,6 +548,16 @@ abbr_class Generates an ```` element with the short name of a PHP class (the FQCN will be shown in a tooltip when a user hovers over the element). +.. code-block:: twig + + {{ 'App\\Entity\\Product'|abbr_class }} + +The above example will be rendered as: + +.. code-block:: html + + Product + abbr_method ~~~~~~~~~~~ @@ -490,6 +572,16 @@ Generates an ```` element using the ``FQCN::method()`` syntax. If ``method`` is ``Closure``, ``Closure`` will be used instead and if ``method`` doesn't have a class name, it's shown as a function (``method()``). +.. code-block:: twig + + {{ 'App\\Controller\\ProductController::list'|abbr_method }} + +The above example will be rendered as: + +.. code-block:: html + + ProductController + format_args ~~~~~~~~~~~ @@ -621,6 +713,21 @@ serialize Accepts any data that can be serialized by the :doc:`Serializer component ` and returns a serialized string in the specified ``format``. +For example:: + + $object = new \stdClass(); + $object->foo = 'bar'; + $object->content = []; + $object->createdAt = new \DateTime('2024-11-30'); + +.. code-block:: twig + + {{ object|serialize(format = 'json', context = { + 'datetime_format': 'D, Y-m-d', + 'empty_array_as_object': true, + }) }} + {# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #} + .. _reference-twig-tags: Tags