@@ -110,6 +110,22 @@ asset
110110``packageName `` *(optional) *
111111 **type **: ``string `` | ``null `` **default **: ``null ``
112112
113+ .. code-block :: yaml
114+
115+ # config/packages/framework.yaml
116+ framework :
117+ # ...
118+ assets :
119+ packages :
120+ foo_package :
121+ base_path : /avatars
122+
123+ .. code-block :: twig
124+
125+ {# the image lives at "public/avatars/avatar.png" #}
126+ {{ asset(path = 'avatar.png', packageName = 'foo_package') }}
127+ {# output: /avatars/avatar.png #}
128+
113129 Returns the public path of the given asset path (which can be a CSS file, a
114130JavaScript file, an image path, etc.). This function takes into account where
115131the application is installed (e.g. in case the project is accessed in a host
@@ -208,6 +224,30 @@ logout_path
208224Generates a relative logout URL for the given firewall. If no key is provided,
209225the URL is generated for the current firewall the user is logged into.
210226
227+ .. code-block :: yaml
228+
229+ # config/packages/security.yaml
230+ security :
231+ # ...
232+
233+ firewalls :
234+ main :
235+ # ...
236+ logout :
237+ path : ' /logout'
238+ othername :
239+ # ...
240+ logout :
241+ path : ' /other/logout'
242+
243+ .. code-block :: twig
244+
245+ {{ logout_path(key = 'main') }}
246+ {# output: /logout #}
247+
248+ {{ logout_path(key = 'othername') }}
249+ {# output: /other/logout #}
250+
211251 logout_url
212252~~~~~~~~~~
213253
@@ -221,6 +261,30 @@ logout_url
221261Equal to the `logout_path `_ function, but it'll generate an absolute URL
222262instead of a relative one.
223263
264+ .. code-block :: yaml
265+
266+ # config/packages/security.yaml
267+ security :
268+ # ...
269+
270+ firewalls :
271+ main :
272+ # ...
273+ logout :
274+ path : ' /logout'
275+ othername :
276+ # ...
277+ logout :
278+ path : ' /other/logout'
279+
280+ .. code-block :: twig
281+
282+ {{ logout_url(key = 'main') }}
283+ {# output: http://example.org/logout #}
284+
285+ {{ logout_url(key = 'othername') }}
286+ {# output: http://example.org/other/logout #}
287+
224288 path
225289~~~~
226290
@@ -238,6 +302,16 @@ path
238302Returns the relative URL (without the scheme and host) for the given route.
239303If ``relative `` is enabled, it'll create a path relative to the current path.
240304
305+ .. code-block :: twig
306+
307+ {# consider that the app defines an 'app_blog' route with the path '/blog/{page}' #}
308+
309+ {{ path(name = 'app_blog', parameters = {page: 3}, relative = false) }}
310+ {# output: /blog/3 #}
311+
312+ {{ path(name = 'app_blog', parameters = {page: 3}, relative = true) }}
313+ {# output: blog/3 #}
314+
241315 .. seealso ::
242316
243317 Read more about :doc: `Symfony routing </routing >` and about
260334Returns the absolute URL (with scheme and host) for the given route. If
261335``schemeRelative `` is enabled, it'll create a scheme-relative URL.
262336
337+ .. code-block :: twig
338+
339+ {# consider that the app defines an 'app_blog' route with the path '/blog/{page}' #}
340+
341+ {{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = false) }}
342+ {# output: http://example.org/blog/3 #}
343+
344+ {{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = true) }}
345+ {# output: //example.org/blog/3 #}
346+
263347 .. seealso ::
264348
265349 Read more about :doc: `Symfony routing </routing >` and about
@@ -311,6 +395,11 @@ expression
311395Creates an :class: `Symfony\\ Component\\ ExpressionLanguage\\ Expression ` related
312396to the :doc: `ExpressionLanguage component </components/expression_language >`.
313397
398+ .. code-block :: twig
399+
400+ {{ expression(1 + 2) }}
401+ {# output: 3 #}
402+
314403 impersonation_path
315404~~~~~~~~~~~~~~~~~~
316405
386475Creates a ``Translatable `` object that can be passed to the
387476:ref: `trans filter <reference-twig-filter-trans >`.
388477
478+ .. configuration-block ::
479+
480+ .. code-block :: yaml
481+
482+ # translations/blog.en.yaml
483+ message : Hello %name%
484+
485+ .. code-block :: xml
486+
487+ <!-- translations/blog.en.xlf -->
488+ <?xml version =" 1.0" encoding =" UTF-8" ?>
489+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
490+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
491+ <body >
492+ <trans-unit id =" message" >
493+ <source >message</source >
494+ <target >Hello %name%</target >
495+ </trans-unit >
496+ </body >
497+ </file >
498+ </xliff >
499+
500+ .. code-block :: php
501+
502+ // translations/blog.en.php
503+ return [
504+ 'message' => "Hello %name%",
505+ ];
506+
507+ Using the filter will be rendered as:
508+
509+ .. code-block :: twig
510+
511+ {{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
512+ {# output: Hello John #}
513+
389514 importmap
390515~~~~~~~~~
391516
@@ -466,6 +591,42 @@ trans
466591Translates the text into the current language. More information in
467592:ref: `Translation Filters <translation-filters >`.
468593
594+ .. configuration-block ::
595+
596+ .. code-block :: yaml
597+
598+ # translations/messages.en.yaml
599+ message : Hello %name%
600+
601+ .. code-block :: xml
602+
603+ <!-- translations/messages.en.xlf -->
604+ <?xml version =" 1.0" encoding =" UTF-8" ?>
605+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
606+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
607+ <body >
608+ <trans-unit id =" message" >
609+ <source >message</source >
610+ <target >Hello %name%</target >
611+ </trans-unit >
612+ </body >
613+ </file >
614+ </xliff >
615+
616+ .. code-block :: php
617+
618+ // translations/messages.en.php
619+ return [
620+ 'message' => "Hello %name%",
621+ ];
622+
623+ Using the filter will be rendered as:
624+
625+ .. code-block :: twig
626+
627+ {{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
628+ {# output: Hello John #}
629+
469630 sanitize_html
470631~~~~~~~~~~~~~
471632
@@ -603,6 +764,16 @@ abbr_class
603764Generates an ``<abbr> `` element with the short name of a PHP class (the
604765FQCN will be shown in a tooltip when a user hovers over the element).
605766
767+ .. code-block :: twig
768+
769+ {{ 'App\\Entity\\Product'|abbr_class }}
770+
771+ The above example will be rendered as:
772+
773+ .. code-block :: html
774+
775+ <abbr title =" App\Entity\Product" >Product</abbr >
776+
606777abbr_method
607778~~~~~~~~~~~
608779
@@ -617,6 +788,16 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
617788``method `` is ``Closure ``, ``Closure `` will be used instead and if ``method ``
618789doesn't have a class name, it's shown as a function (``method() ``).
619790
791+ .. code-block :: twig
792+
793+ {{ 'App\\Controller\\ProductController::list'|abbr_method }}
794+
795+ The above example will be rendered as:
796+
797+ .. code-block :: html
798+
799+ <abbr title =" App\Controller\ProductController" >ProductController</abbr >::list()
800+
620801format_args
621802~~~~~~~~~~~
622803
@@ -704,6 +885,11 @@ file_link
704885Generates a link to the provided file and line number using
705886a preconfigured scheme.
706887
888+ .. code-block :: twig
889+
890+ {{ 'path/to/file/file.txt'|file_link(line = 3) }}
891+ {# output: file://path/to/file/file.txt#L3 #}
892+
707893 file_relative
708894~~~~~~~~~~~~~
709895
@@ -746,6 +932,21 @@ serialize
746932Accepts any data that can be serialized by the :doc: `Serializer component </serializer >`
747933and returns a serialized string in the specified ``format ``.
748934
935+ For example::
936+
937+ $object = new \stdClass();
938+ $object->foo = 'bar';
939+ $object->content = [];
940+ $object->createdAt = new \DateTime('2024-11-30');
941+
942+ .. code-block :: twig
943+
944+ {{ object|serialize(format = 'json', context = {
945+ 'datetime_format': 'D, Y-m-d',
946+ 'empty_array_as_object': true,
947+ }) }}
948+ {# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
949+
749950 .. _reference-twig-filter-emojify :
750951
751952emojify
0 commit comments