@@ -110,6 +110,22 @@ asset
110
110
``packageName `` *(optional) *
111
111
**type **: ``string `` | ``null `` **default **: ``null ``
112
112
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
+
113
129
Returns the public path of the given asset path (which can be a CSS file, a
114
130
JavaScript file, an image path, etc.). This function takes into account where
115
131
the application is installed (e.g. in case the project is accessed in a host
@@ -187,6 +203,30 @@ logout_path
187
203
Generates a relative logout URL for the given firewall. If no key is provided,
188
204
the URL is generated for the current firewall the user is logged into.
189
205
206
+ .. code-block :: yaml
207
+
208
+ # config/packages/security.yaml
209
+ security :
210
+ # ...
211
+
212
+ firewalls :
213
+ main :
214
+ # ...
215
+ logout :
216
+ path : ' /logout'
217
+ othername :
218
+ # ...
219
+ logout :
220
+ path : ' /other/logout'
221
+
222
+ .. code-block :: twig
223
+
224
+ {{ logout_path(key = 'main') }}
225
+ {# output: /logout #}
226
+
227
+ {{ logout_path(key = 'othername') }}
228
+ {# output: /other/logout #}
229
+
190
230
logout_url
191
231
~~~~~~~~~~
192
232
@@ -200,6 +240,30 @@ logout_url
200
240
Equal to the `logout_path `_ function, but it'll generate an absolute URL
201
241
instead of a relative one.
202
242
243
+ .. code-block :: yaml
244
+
245
+ # config/packages/security.yaml
246
+ security :
247
+ # ...
248
+
249
+ firewalls :
250
+ main :
251
+ # ...
252
+ logout :
253
+ path : ' /logout'
254
+ othername :
255
+ # ...
256
+ logout :
257
+ path : ' /other/logout'
258
+
259
+ .. code-block :: twig
260
+
261
+ {{ logout_url(key = 'main') }}
262
+ {# output: http://example.org/logout #}
263
+
264
+ {{ logout_url(key = 'othername') }}
265
+ {# output: http://example.org/other/logout #}
266
+
203
267
path
204
268
~~~~
205
269
@@ -217,6 +281,14 @@ path
217
281
Returns the relative URL (without the scheme and host) for the given route.
218
282
If ``relative `` is enabled, it'll create a path relative to the current path.
219
283
284
+ .. code-block :: twig
285
+
286
+ {{ path(name = 'app_blog', parameters = {page: 3}, relative = false) }}
287
+ {# output (depending on the route configuration): /blog/3 or /blog?page=3 #}
288
+
289
+ {{ path(name = 'app_blog', parameters = {page: 3}, relative = true) }}
290
+ {# output (depending on the route configuration): blog/3 or ?page=3 #}
291
+
220
292
.. seealso ::
221
293
222
294
Read more about :doc: `Symfony routing </routing >` and about
239
311
Returns the absolute URL (with scheme and host) for the given route. If
240
312
``schemeRelative `` is enabled, it'll create a scheme-relative URL.
241
313
314
+ .. code-block :: twig
315
+
316
+ {{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = false) }}
317
+ {# output (depending on the route configuration): http://example.org/blog/3
318
+ or http://example.org/blog?page=3 #}
319
+
320
+ {{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = true) }}
321
+ {# output (depending on the route configuration): //example.org/blog/3
322
+ or //example.org/blog?page=3 #}
323
+
242
324
.. seealso ::
243
325
244
326
Read more about :doc: `Symfony routing </routing >` and about
@@ -290,6 +372,11 @@ expression
290
372
Creates an :class: `Symfony\\ Component\\ ExpressionLanguage\\ Expression ` related
291
373
to the :doc: `ExpressionLanguage component </components/expression_language >`.
292
374
375
+ .. code-block :: twig
376
+
377
+ {{ expression(1 + 2) }}
378
+ {# output: 3 #}
379
+
293
380
impersonation_path
294
381
~~~~~~~~~~~~~~~~~~
295
382
373
460
Creates a ``Translatable `` object that can be passed to the
374
461
:ref: `trans filter <reference-twig-filter-trans >`.
375
462
463
+ .. configuration-block ::
464
+
465
+ .. code-block :: yaml
466
+
467
+ # translations/blog.en.yaml
468
+ message : Hello %name%
469
+
470
+ .. code-block :: xml
471
+
472
+ <!-- translations/blog.en.xlf -->
473
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
474
+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
475
+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
476
+ <body >
477
+ <trans-unit id =" message" >
478
+ <source >message</source >
479
+ <target >Hello %name%</target >
480
+ </trans-unit >
481
+ </body >
482
+ </file >
483
+ </xliff >
484
+
485
+ .. code-block :: php
486
+
487
+ // translations/blog.en.php
488
+ return [
489
+ 'message' => "Hello %name%",
490
+ ];
491
+
492
+ Using the filter will be rendered as:
493
+
494
+ .. code-block :: twig
495
+
496
+ {{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
497
+ {# output: Hello John #}
498
+
376
499
importmap
377
500
~~~~~~~~~
378
501
@@ -452,6 +575,42 @@ trans
452
575
Translates the text into the current language. More information in
453
576
:ref: `Translation Filters <translation-filters >`.
454
577
578
+ .. configuration-block ::
579
+
580
+ .. code-block :: yaml
581
+
582
+ # translations/messages.en.yaml
583
+ message : Hello %name%
584
+
585
+ .. code-block :: xml
586
+
587
+ <!-- translations/messages.en.xlf -->
588
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
589
+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
590
+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
591
+ <body >
592
+ <trans-unit id =" message" >
593
+ <source >message</source >
594
+ <target >Hello %name%</target >
595
+ </trans-unit >
596
+ </body >
597
+ </file >
598
+ </xliff >
599
+
600
+ .. code-block :: php
601
+
602
+ // translations/messages.en.php
603
+ return [
604
+ 'message' => "Hello %name%",
605
+ ];
606
+
607
+ Using the filter will be rendered as:
608
+
609
+ .. code-block :: twig
610
+
611
+ {{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
612
+ {# output: Hello John #}
613
+
455
614
sanitize_html
456
615
~~~~~~~~~~~~~
457
616
@@ -593,6 +752,16 @@ abbr_class
593
752
Generates an ``<abbr> `` element with the short name of a PHP class (the
594
753
FQCN will be shown in a tooltip when a user hovers over the element).
595
754
755
+ .. code-block :: twig
756
+
757
+ {{ 'App\\Entity\\Product'|abbr_class }}
758
+
759
+ The above example will be rendered as:
760
+
761
+ .. code-block :: html
762
+
763
+ <abbr title =" App\Entity\Product" >Product</abbr >
764
+
596
765
abbr_method
597
766
~~~~~~~~~~~
598
767
@@ -607,6 +776,16 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
607
776
``method `` is ``Closure ``, ``Closure `` will be used instead and if ``method ``
608
777
doesn't have a class name, it's shown as a function (``method() ``).
609
778
779
+ .. code-block :: twig
780
+
781
+ {{ 'App\\Controller\\ProductController::list'|abbr_method }}
782
+
783
+ The above example will be rendered as:
784
+
785
+ .. code-block :: html
786
+
787
+ <abbr title =" App\Controller\ProductController" >ProductController</abbr >
788
+
610
789
format_args
611
790
~~~~~~~~~~~
612
791
@@ -694,6 +873,11 @@ file_link
694
873
Generates a link to the provided file and line number using
695
874
a preconfigured scheme.
696
875
876
+ .. code-block :: twig
877
+
878
+ {{ 'path/to/file/file.txt'|file_link(line = 3) }}
879
+ {# output: file://path/to/file/file.txt#L3 #}
880
+
697
881
file_relative
698
882
~~~~~~~~~~~~~
699
883
@@ -736,6 +920,21 @@ serialize
736
920
Accepts any data that can be serialized by the :doc: `Serializer component </serializer >`
737
921
and returns a serialized string in the specified ``format ``.
738
922
923
+ For example::
924
+
925
+ $object = new \stdClass();
926
+ $object->foo = 'bar';
927
+ $object->content = [];
928
+ $object->createdAt = new \DateTime('2024-11-30');
929
+
930
+ .. code-block :: twig
931
+
932
+ {{ object|serialize(format = 'json', context = {
933
+ 'datetime_format': 'D, Y-m-d',
934
+ 'empty_array_as_object': true,
935
+ }) }}
936
+ {# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
937
+
739
938
.. _reference-twig-tags :
740
939
741
940
Tags
0 commit comments