Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twig] [twig reference] add examples to functions and filter #20418

Open
wants to merge 2 commits into
base: 5.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@
``packageName`` *(optional)*
**type**: ``string`` | ``null`` **default**: ``null``

.. code-block:: yaml

# config/packages/framework.yaml
framework:
# ...
assets:
packages:
foo_package:
base_path: /avatars

.. code-block:: twig

{# the image lives at "public/avatars/avatar.png" #}
{{ asset(path = 'avatar.png', packageName = 'foo_package') }}
{# output: /avatars/avatar.png #}

Returns the public path of the given asset path (which can be a CSS file, a
JavaScript file, an image path, etc.). This function takes into account where
the application is installed (e.g. in case the project is accessed in a host
Expand Down Expand Up @@ -189,6 +205,30 @@
Generates a relative logout URL for the given firewall. If no key is provided,
the URL is generated for the current firewall the user is logged into.

.. code-block:: yaml

# config/packages/security.yaml
security:
# ...

firewalls:
main:
# ...
logout:
path: '/logout'
othername:
# ...
logout:
path: '/other/logout'

Check failure on line 222 in reference/twig_reference.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In SecurityExtension.php line 672: No authentication listener registered for firewall "main".

Check failure on line 222 in reference/twig_reference.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In SecurityExtension.php line 672: No authentication listener registered for firewall "main".

.. code-block:: twig

{{ logout_path(key = 'main') }}
{# output: /logout #}

{{ logout_path(key = 'othername') }}
{# output: /other/logout #}

logout_url
~~~~~~~~~~

Expand All @@ -202,6 +242,30 @@
Equal to the `logout_path`_ function, but it'll generate an absolute URL
instead of a relative one.

.. code-block:: yaml

# config/packages/security.yaml
security:
# ...

firewalls:
main:
# ...
logout:
path: '/logout'
othername:
# ...
logout:
path: '/other/logout'

.. code-block:: twig

{{ logout_url(key = 'main') }}
{# output: http://example.org/logout #}

{{ logout_url(key = 'othername') }}
{# output: http://example.org/other/logout #}

path
~~~~

Expand All @@ -219,6 +283,14 @@
Returns the relative URL (without the scheme and host) for the given route.
If ``relative`` is enabled, it'll create a path relative to the current path.

.. code-block:: twig

{{ path(name = 'app_blog', parameters = {page: 3}, relative = false) }}
{# output (depending on the route configuration): /blog/3 or /blog?page=3 #}

{{ path(name = 'app_blog', parameters = {page: 3}, relative = true) }}
{# output (depending on the route configuration): blog/3 or ?page=3 #}

.. seealso::

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

.. code-block:: twig

{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = false) }}
{# output (depending on the route configuration): http://example.org/blog/3
or http://example.org/blog?page=3 #}

{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = true) }}
{# output (depending on the route configuration): //example.org/blog/3
or //example.org/blog?page=3 #}

.. seealso::

Read more about :doc:`Symfony routing </routing>` and about
Expand Down Expand Up @@ -292,6 +374,11 @@
Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` related
to the :doc:`ExpressionLanguage component </components/expression_language>`.

.. code-block:: twig

{{ expression(1 + 2) }}
{# output: 3 #}

impersonation_exit_path
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -352,6 +439,42 @@
Creates a ``Translatable`` object that can be passed to the
:ref:`trans filter <reference-twig-filter-trans>`.

.. configuration-block::

.. code-block:: yaml

# translations/blog.en.yaml
message: Hello %name%

.. code-block:: xml

<!-- translations/blog.en.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="message">
<source>message</source>
<target>Hello %name%</target>
</trans-unit>
</body>
</file>
</xliff>

.. code-block:: php

// translations/blog.en.php
return [
'message' => "Hello %name%",
];

Using the filter will be rendered as:

.. code-block:: twig

{{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
{# output: Hello John #}

Form Related Functions
~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -429,6 +552,42 @@
Translates the text into the current language. More information in
:ref:`Translation Filters <translation-filters>`.

.. configuration-block::

.. code-block:: yaml

# translations/messages.en.yaml
message: Hello %name%

.. code-block:: xml

<!-- translations/messages.en.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="message">
<source>message</source>
<target>Hello %name%</target>
</trans-unit>
</body>
</file>
</xliff>

.. code-block:: php

// translations/messages.en.php
return [
'message' => "Hello %name%",
];

Using the filter will be rendered as:

.. code-block:: twig

{{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
{# output: Hello John #}

yaml_encode
~~~~~~~~~~~

Expand Down Expand Up @@ -476,6 +635,16 @@
Generates an ``<abbr>`` element with the short name of a PHP class (the
FQCN will be shown in a tooltip when a user hovers over the element).

.. code-block:: twig

{{ 'App\\Entity\\Product'|abbr_class }}

The above example will be rendered as:

.. code-block:: html

<abbr title="App\Entity\Product">Product</abbr>

abbr_method
~~~~~~~~~~~

Expand All @@ -490,6 +659,16 @@
``method`` is ``Closure``, ``Closure`` will be used instead and if ``method``
doesn't have a class name, it's shown as a function (``method()``).

.. code-block:: twig

{{ 'App\\Controller\\ProductController::list'|abbr_method }}

The above example will be rendered as:

.. code-block:: html

<abbr title="App\Controller\ProductController">ProductController</abbr>

format_args
~~~~~~~~~~~

Expand Down Expand Up @@ -577,6 +756,11 @@
Generates a link to the provided file and line number using
a preconfigured scheme.

.. code-block:: twig

{{ 'path/to/file/file.txt'|file_link(line = 3) }}
{# output: file://path/to/file/file.txt#L3 #}

file_relative
~~~~~~~~~~~~~

Expand Down Expand Up @@ -621,6 +805,21 @@
Accepts any data that can be serialized by the :doc:`Serializer component </serializer>`
and returns a serialized string in the specified ``format``.

For example::

$object = new \stdClass();
$object->foo = 'bar';
$object->content = [];
$object->createdAt = new \DateTime('2024-11-30');

.. code-block:: twig

{{ object|serialize(format = 'json', context = {
'datetime_format': 'D, Y-m-d',
'empty_array_as_object': true,
}) }}
{# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}

.. _reference-twig-tags:

Tags
Expand Down
Loading