Skip to content

Commit b05723c

Browse files
committed
[Twig] [twig reference] add examples to function and filters
1 parent e134083 commit b05723c

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Diff for: reference/twig_reference.rst

+107
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,42 @@ t
352352
Creates a ``Translatable`` object that can be passed to the
353353
:ref:`trans filter <reference-twig-filter-trans>`.
354354

355+
.. configuration-block::
356+
357+
.. code-block:: yaml
358+
359+
# translations/blog.en.yaml
360+
message: Hello %name%
361+
362+
.. code-block:: xml
363+
364+
<!-- translations/blog.en.xlf -->
365+
<?xml version="1.0" encoding="UTF-8" ?>
366+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
367+
<file source-language="en" datatype="plaintext" original="file.ext">
368+
<body>
369+
<trans-unit id="message">
370+
<source>message</source>
371+
<target>Hello %name%</target>
372+
</trans-unit>
373+
</body>
374+
</file>
375+
</xliff>
376+
377+
.. code-block:: php
378+
379+
// translations/blog.en.php
380+
return [
381+
'message' => "Hello %name%",
382+
];
383+
384+
Using the filter will be rendered as:
385+
386+
.. code-block:: twig
387+
388+
{{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
389+
{# output: Hello John #}
390+
355391
Form Related Functions
356392
~~~~~~~~~~~~~~~~~~~~~~
357393

@@ -429,6 +465,42 @@ trans
429465
Translates the text into the current language. More information in
430466
:ref:`Translation Filters <translation-filters>`.
431467

468+
.. configuration-block::
469+
470+
.. code-block:: yaml
471+
472+
# translations/messages.en.yaml
473+
message: Hello %name%
474+
475+
.. code-block:: xml
476+
477+
<!-- translations/messages.en.xlf -->
478+
<?xml version="1.0" encoding="UTF-8" ?>
479+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
480+
<file source-language="en" datatype="plaintext" original="file.ext">
481+
<body>
482+
<trans-unit id="message">
483+
<source>message</source>
484+
<target>Hello %name%</target>
485+
</trans-unit>
486+
</body>
487+
</file>
488+
</xliff>
489+
490+
.. code-block:: php
491+
492+
// translations/messages.en.php
493+
return [
494+
'message' => "Hello %name%",
495+
];
496+
497+
Using the filter will be rendered as:
498+
499+
.. code-block:: twig
500+
501+
{{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
502+
{# output: Hello John #}
503+
432504
yaml_encode
433505
~~~~~~~~~~~
434506

@@ -476,6 +548,16 @@ abbr_class
476548
Generates an ``<abbr>`` element with the short name of a PHP class (the
477549
FQCN will be shown in a tooltip when a user hovers over the element).
478550

551+
.. code-block:: twig
552+
553+
{{ 'App\\Entity\\Product'|abbr_class }}
554+
555+
The above example will be rendered as:
556+
557+
.. code-block:: html
558+
559+
<abbr title="App\Entity\Product">Product</abbr>
560+
479561
abbr_method
480562
~~~~~~~~~~~
481563

@@ -490,6 +572,16 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
490572
``method`` is ``Closure``, ``Closure`` will be used instead and if ``method``
491573
doesn't have a class name, it's shown as a function (``method()``).
492574

575+
.. code-block:: twig
576+
577+
{{ 'App\\Controller\\ProductController::list'|abbr_method }}
578+
579+
The above example will be rendered as:
580+
581+
.. code-block:: html
582+
583+
<abbr title="App\Controller\ProductController">ProductController</abbr>
584+
493585
format_args
494586
~~~~~~~~~~~
495587

@@ -621,6 +713,21 @@ serialize
621713
Accepts any data that can be serialized by the :doc:`Serializer component </serializer>`
622714
and returns a serialized string in the specified ``format``.
623715

716+
For example::
717+
718+
$object = new \stdClass();
719+
$object->foo = 'bar';
720+
$object->content = [];
721+
$object->createdAt = new \DateTime('2024-11-30');
722+
723+
.. code-block:: twig
724+
725+
{{ object|serialize(format = 'json', context = {
726+
'datetime_format': 'D, Y-m-d',
727+
'empty_array_as_object': true,
728+
}) }}
729+
{# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
730+
624731
.. _reference-twig-tags:
625732

626733
Tags

0 commit comments

Comments
 (0)