Skip to content

Commit d71855e

Browse files
committed
Merge branch '6.4' into 7.2
* 6.4: Minor tweaks [Twig] [twig reference] add examples to functions and filter
2 parents 572533f + f187ace commit d71855e

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

reference/twig_reference.rst

+201
Original file line numberDiff line numberDiff line change
@@ -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
114130
JavaScript file, an image path, etc.). This function takes into account where
115131
the application is installed (e.g. in case the project is accessed in a host
@@ -187,6 +203,30 @@ logout_path
187203
Generates a relative logout URL for the given firewall. If no key is provided,
188204
the URL is generated for the current firewall the user is logged into.
189205

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+
190230
logout_url
191231
~~~~~~~~~~
192232

@@ -200,6 +240,30 @@ logout_url
200240
Equal to the `logout_path`_ function, but it'll generate an absolute URL
201241
instead of a relative one.
202242

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+
203267
path
204268
~~~~
205269

@@ -217,6 +281,16 @@ path
217281
Returns the relative URL (without the scheme and host) for the given route.
218282
If ``relative`` is enabled, it'll create a path relative to the current path.
219283

284+
.. code-block:: twig
285+
286+
{# consider that the app defines an 'app_blog' route with the path '/blog/{page}' #}
287+
288+
{{ path(name = 'app_blog', parameters = {page: 3}, relative = false) }}
289+
{# output: /blog/3 #}
290+
291+
{{ path(name = 'app_blog', parameters = {page: 3}, relative = true) }}
292+
{# output: blog/3 #}
293+
220294
.. seealso::
221295

222296
Read more about :doc:`Symfony routing </routing>` and about
@@ -239,6 +313,16 @@ url
239313
Returns the absolute URL (with scheme and host) for the given route. If
240314
``schemeRelative`` is enabled, it'll create a scheme-relative URL.
241315

316+
.. code-block:: twig
317+
318+
{# consider that the app defines an 'app_blog' route with the path '/blog/{page}' #}
319+
320+
{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = false) }}
321+
{# output: http://example.org/blog/3 #}
322+
323+
{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = true) }}
324+
{# output: //example.org/blog/3 #}
325+
242326
.. seealso::
243327

244328
Read more about :doc:`Symfony routing </routing>` and about
@@ -290,6 +374,11 @@ expression
290374
Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` related
291375
to the :doc:`ExpressionLanguage component </components/expression_language>`.
292376

377+
.. code-block:: twig
378+
379+
{{ expression(1 + 2) }}
380+
{# output: 3 #}
381+
293382
impersonation_path
294383
~~~~~~~~~~~~~~~~~~
295384

@@ -365,6 +454,42 @@ t
365454
Creates a ``Translatable`` object that can be passed to the
366455
:ref:`trans filter <reference-twig-filter-trans>`.
367456

457+
.. configuration-block::
458+
459+
.. code-block:: yaml
460+
461+
# translations/blog.en.yaml
462+
message: Hello %name%
463+
464+
.. code-block:: xml
465+
466+
<!-- translations/blog.en.xlf -->
467+
<?xml version="1.0" encoding="UTF-8" ?>
468+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
469+
<file source-language="en" datatype="plaintext" original="file.ext">
470+
<body>
471+
<trans-unit id="message">
472+
<source>message</source>
473+
<target>Hello %name%</target>
474+
</trans-unit>
475+
</body>
476+
</file>
477+
</xliff>
478+
479+
.. code-block:: php
480+
481+
// translations/blog.en.php
482+
return [
483+
'message' => "Hello %name%",
484+
];
485+
486+
Using the filter will be rendered as:
487+
488+
.. code-block:: twig
489+
490+
{{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
491+
{# output: Hello John #}
492+
368493
importmap
369494
~~~~~~~~~
370495

@@ -444,6 +569,42 @@ trans
444569
Translates the text into the current language. More information in
445570
:ref:`Translation Filters <translation-filters>`.
446571

572+
.. configuration-block::
573+
574+
.. code-block:: yaml
575+
576+
# translations/messages.en.yaml
577+
message: Hello %name%
578+
579+
.. code-block:: xml
580+
581+
<!-- translations/messages.en.xlf -->
582+
<?xml version="1.0" encoding="UTF-8" ?>
583+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
584+
<file source-language="en" datatype="plaintext" original="file.ext">
585+
<body>
586+
<trans-unit id="message">
587+
<source>message</source>
588+
<target>Hello %name%</target>
589+
</trans-unit>
590+
</body>
591+
</file>
592+
</xliff>
593+
594+
.. code-block:: php
595+
596+
// translations/messages.en.php
597+
return [
598+
'message' => "Hello %name%",
599+
];
600+
601+
Using the filter will be rendered as:
602+
603+
.. code-block:: twig
604+
605+
{{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
606+
{# output: Hello John #}
607+
447608
sanitize_html
448609
~~~~~~~~~~~~~
449610

@@ -581,6 +742,16 @@ abbr_class
581742
Generates an ``<abbr>`` element with the short name of a PHP class (the
582743
FQCN will be shown in a tooltip when a user hovers over the element).
583744

745+
.. code-block:: twig
746+
747+
{{ 'App\\Entity\\Product'|abbr_class }}
748+
749+
The above example will be rendered as:
750+
751+
.. code-block:: html
752+
753+
<abbr title="App\Entity\Product">Product</abbr>
754+
584755
abbr_method
585756
~~~~~~~~~~~
586757

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

769+
.. code-block:: twig
770+
771+
{{ 'App\\Controller\\ProductController::list'|abbr_method }}
772+
773+
The above example will be rendered as:
774+
775+
.. code-block:: html
776+
777+
<abbr title="App\Controller\ProductController">ProductController</abbr>::list()
778+
598779
format_args
599780
~~~~~~~~~~~
600781

@@ -682,6 +863,11 @@ file_link
682863
Generates a link to the provided file and line number using
683864
a preconfigured scheme.
684865

866+
.. code-block:: twig
867+
868+
{{ 'path/to/file/file.txt'|file_link(line = 3) }}
869+
{# output: file://path/to/file/file.txt#L3 #}
870+
685871
file_relative
686872
~~~~~~~~~~~~~
687873

@@ -724,6 +910,21 @@ serialize
724910
Accepts any data that can be serialized by the :doc:`Serializer component </serializer>`
725911
and returns a serialized string in the specified ``format``.
726912

913+
For example::
914+
915+
$object = new \stdClass();
916+
$object->foo = 'bar';
917+
$object->content = [];
918+
$object->createdAt = new \DateTime('2024-11-30');
919+
920+
.. code-block:: twig
921+
922+
{{ object|serialize(format = 'json', context = {
923+
'datetime_format': 'D, Y-m-d',
924+
'empty_array_as_object': true,
925+
}) }}
926+
{# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
927+
727928
.. _reference-twig-filter-emojify:
728929

729930
emojify

0 commit comments

Comments
 (0)