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 (format_file, file_… #20423

Open
wants to merge 4 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
72 changes: 72 additions & 0 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ like :ref:`render() <reference-twig-function-render>` and

.. _reference-twig-function-asset:

.. code-block:: html+twig

{% set myArray = {'a': 'foo', 'b': 'bar'} %}

<iframe src="{{ render(controller('App\\Controller\\MyController::baz', {'myArray': myArray})) }}"></iframe>

Output:

.. code-block:: html

<iframe src="<ul>
<li>foo</li>
<li>bar</li>
</ul>"></iframe>

asset
~~~~~

Expand Down Expand Up @@ -157,6 +172,11 @@ csrf_token
Renders a CSRF token. Use this function if you want :doc:`CSRF protection </security/csrf>`
in a regular HTML form not managed by the Symfony Form component.

.. code-block:: twig

{{ csrf_token(intention = 'my_form') }}
{# output: generates a variable token #}

is_granted
~~~~~~~~~~

Expand Down Expand Up @@ -532,6 +552,28 @@ Generates an excerpt of a code file around the given ``line`` number. The
``srcContext`` argument defines the total number of lines to display around the
given line number (use ``-1`` to display the whole file).

Let's assume this is the content of a file :

.. code-block:: text

a
b
c
d
e

.. code-block:: twig

{{ "/path/to/file/file.txt"|file_excerpt(line = 4, srcContext = 1) }}
{# output:
3.c
4.d
5.e #}

{{ "/path/to/file/file.txt"|file_excerpt(line = 1, srcContext = 0) }}
{# output:
1.a #}

format_file
~~~~~~~~~~~

Expand All @@ -550,6 +592,36 @@ Generates the file path inside an ``<a>`` element. If the path is inside
the kernel root directory, the kernel root directory path is replaced by
``kernel.project_dir`` (showing the full path in a tooltip on hover).

Example 1

.. code-block:: twig

{{ "path/to/file/file.txt"|format_file(line = 1, text = "my_text") }}

Output:

.. code-block:: html

<a href="path/to/file/file.txt#L1"
title="Click to open this file" class="file_link">my_text at line 1
</a>

Example 2

.. code-block:: twig

{{ "path/to/file/file.txt"|format_file(line = 3) }}

Output:

.. code-block:: html

<a href="path/to/file/file.txt#L3"
title="Click to open this file" class="file_link">
<abbr title="path/to/file/file.txt">file.txt</abbr>
/ at line 3
</a>

format_file_from_text
~~~~~~~~~~~~~~~~~~~~~

Expand Down