352
352
Creates a ``Translatable `` object that can be passed to the
353
353
:ref: `trans filter <reference-twig-filter-trans >`.
354
354
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
+
355
391
Form Related Functions
356
392
~~~~~~~~~~~~~~~~~~~~~~
357
393
@@ -429,6 +465,42 @@ trans
429
465
Translates the text into the current language. More information in
430
466
:ref: `Translation Filters <translation-filters >`.
431
467
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
+
432
504
yaml_encode
433
505
~~~~~~~~~~~
434
506
@@ -476,6 +548,16 @@ abbr_class
476
548
Generates an ``<abbr> `` element with the short name of a PHP class (the
477
549
FQCN will be shown in a tooltip when a user hovers over the element).
478
550
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
+
479
561
abbr_method
480
562
~~~~~~~~~~~
481
563
@@ -490,6 +572,16 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
490
572
``method `` is ``Closure ``, ``Closure `` will be used instead and if ``method ``
491
573
doesn't have a class name, it's shown as a function (``method() ``).
492
574
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
+
493
585
format_args
494
586
~~~~~~~~~~~
495
587
@@ -621,6 +713,21 @@ serialize
621
713
Accepts any data that can be serialized by the :doc: `Serializer component </serializer >`
622
714
and returns a serialized string in the specified ``format ``.
623
715
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
+
624
731
.. _reference-twig-tags :
625
732
626
733
Tags
0 commit comments