Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 850666f

Browse filesBrowse files
Sarah-eitjaviereguiluz
authored andcommitted
[Twig] [twig reference] add examples to functions and filter
1 parent 35c408c commit 850666f
Copy full SHA for 850666f

File tree

Expand file treeCollapse file tree

1 file changed

+199
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+199
-0
lines changed

‎reference/twig_reference.rst

Copy file name to clipboardExpand all lines: reference/twig_reference.rst
+199Lines changed: 199 additions & 0 deletions
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,14 @@ 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+
{{ 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+
220292
.. seealso::
221293

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

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+
242324
.. seealso::
243325

244326
Read more about :doc:`Symfony routing </routing>` and about
@@ -290,6 +372,11 @@ expression
290372
Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` related
291373
to the :doc:`ExpressionLanguage component </components/expression_language>`.
292374

375+
.. code-block:: twig
376+
377+
{{ expression(1 + 2) }}
378+
{# output: 3 #}
379+
293380
impersonation_path
294381
~~~~~~~~~~~~~~~~~~
295382

@@ -373,6 +460,42 @@ t
373460
Creates a ``Translatable`` object that can be passed to the
374461
:ref:`trans filter <reference-twig-filter-trans>`.
375462

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+
376499
importmap
377500
~~~~~~~~~
378501

@@ -452,6 +575,42 @@ trans
452575
Translates the text into the current language. More information in
453576
:ref:`Translation Filters <translation-filters>`.
454577

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+
455614
sanitize_html
456615
~~~~~~~~~~~~~
457616

@@ -593,6 +752,16 @@ abbr_class
593752
Generates an ``<abbr>`` element with the short name of a PHP class (the
594753
FQCN will be shown in a tooltip when a user hovers over the element).
595754

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+
596765
abbr_method
597766
~~~~~~~~~~~
598767

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

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+
610789
format_args
611790
~~~~~~~~~~~
612791

@@ -694,6 +873,11 @@ file_link
694873
Generates a link to the provided file and line number using
695874
a preconfigured scheme.
696875

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+
697881
file_relative
698882
~~~~~~~~~~~~~
699883

@@ -736,6 +920,21 @@ serialize
736920
Accepts any data that can be serialized by the :doc:`Serializer component </serializer>`
737921
and returns a serialized string in the specified ``format``.
738922

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+
739938
.. _reference-twig-tags:
740939

741940
Tags

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.