Skip to content

Commit 82a75e5

Browse files
committed
[Twig] [twig reference] add examples to functions (format_file, file_exercp, fragment_uri, controller, csrf_token)
1 parent 29c83bd commit 82a75e5

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Diff for: reference/twig_reference.rst

+101
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ Generates the URI of :ref:`a fragment <fragments-path-config>`.
8181
.. versionadded:: 5.3
8282

8383
The ``fragment_uri()`` function was introduced in Symfony 5.3.
84+
Example 1
85+
86+
.. code-block:: twig
87+
88+
{{ fragment_uri(controller('App\\Controller\\HomeController::index'),
89+
absolute = true, strict = true, sign = true) }}
90+
91+
Output:
92+
93+
.. code-block:: html
94+
95+
<a href="https://example.com/_fragment?_path=_controller=App%5CController%5CHomeController%3A%3Aindex&_hash=abcdef123456">
96+
https://example.com/_fragment?_path=_controller=App%5CController%5CHomeController%3A%3Aindex&_hash=abcdef123456
97+
</a>
98+
99+
Example 2
100+
101+
.. code-block:: twig
102+
103+
{{ fragment_uri(controller('App\\Controller\\UserController::profile',
104+
{ 'id': 42 }), absolute = false, strict = false, sign = false) }}
105+
106+
Output:
107+
108+
.. code-block:: html
109+
110+
<a href="/_fragment?_path=_controller=App%5CController%5CUserController%3A%3Aprofile&id=42">
111+
/_fragment?_path=_controller=App%5CController%5CUserController%3A%3Aprofile&id=42
112+
</a>
84113

85114
controller
86115
~~~~~~~~~~
@@ -102,6 +131,21 @@ like :ref:`render() <reference-twig-function-render>` and
102131

103132
.. _reference-twig-function-asset:
104133

134+
.. code-block:: twig
135+
136+
{% set myArray = {'a': 'foo', 'b': 'bar'} %}
137+
138+
<iframe src="{{ render(controller('App\\Controller\\MyController::baz', {'myArray': myArray})) }}"></iframe>
139+
140+
Output:
141+
142+
.. code-block:: html
143+
144+
<iframe src="<ul>
145+
<li>foo</li>
146+
<li>bar</li>
147+
</ul>"></iframe>
148+
105149
asset
106150
~~~~~
107151

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

204+
.. code-block:: twig
205+
206+
{{ csrf_token(intention = 'my_form') }}
207+
{# output: generates a variable token #}
208+
160209
is_granted
161210
~~~~~~~~~~
162211

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

584+
Let's assume this is the content of a file :
585+
586+
.. code-block:: text
587+
588+
a
589+
b
590+
c
591+
d
592+
e
593+
594+
.. code-block:: twig
595+
596+
{{ "/path/to/file/file.txt"|file_excerpt(line = 4, srcContext = 1) }}
597+
{# output:
598+
3.c
599+
4.d
600+
5.e #}
601+
602+
{{ "/path/to/file/file.txt"|file_excerpt(line = 1, srcContext = 0) }}
603+
{# output:
604+
1.a #}
605+
535606
format_file
536607
~~~~~~~~~~~
537608

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

624+
Example 1
625+
626+
.. code-block:: twig
627+
628+
{{ "path/to/file/file.txt"|format_file(line = 1, text = "my_text") }}
629+
630+
Output:
631+
632+
.. code-block:: html
633+
634+
<a href="path/to/file/file.txt#L1"
635+
title="Click to open this file" class="file_link">my_text at line 1
636+
</a>
637+
638+
Example 2
639+
640+
.. code-block:: twig
641+
642+
{{ "path/to/file/file.txt"|format_file(line = 3) }}
643+
644+
Output:
645+
646+
.. code-block:: html
647+
648+
<a href="path/to/file/file.txt#L3"
649+
title="Click to open this file" class="file_link">
650+
<abbr title="path/to/file/file.txt">file.txt</abbr>
651+
/ at line 3
652+
</a>
653+
553654
format_file_from_text
554655
~~~~~~~~~~~~~~~~~~~~~
555656

0 commit comments

Comments
 (0)