From 82a75e5e298ebe876f2875dd3d7fe6d8562bfe20 Mon Sep 17 00:00:00 2001 From: Edgar Brunet Date: Fri, 29 Nov 2024 14:45:51 +0100 Subject: [PATCH 1/4] [Twig] [twig reference] add examples to functions (format_file, file_exercp, fragment_uri, controller, csrf_token) --- reference/twig_reference.rst | 101 +++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 1c20264352c..523a0e87705 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -81,6 +81,35 @@ Generates the URI of :ref:`a fragment `. .. versionadded:: 5.3 The ``fragment_uri()`` function was introduced in Symfony 5.3. +Example 1 + +.. code-block:: twig + + {{ fragment_uri(controller('App\\Controller\\HomeController::index'), + absolute = true, strict = true, sign = true) }} + +Output: + +.. code-block:: html + + + https://example.com/_fragment?_path=_controller=App%5CController%5CHomeController%3A%3Aindex&_hash=abcdef123456 + + +Example 2 + +.. code-block:: twig + + {{ fragment_uri(controller('App\\Controller\\UserController::profile', + { 'id': 42 }), absolute = false, strict = false, sign = false) }} + +Output: + +.. code-block:: html + + + /_fragment?_path=_controller=App%5CController%5CUserController%3A%3Aprofile&id=42 + controller ~~~~~~~~~~ @@ -102,6 +131,21 @@ like :ref:`render() ` and .. _reference-twig-function-asset: +.. code-block:: twig + + {% set myArray = {'a': 'foo', 'b': 'bar'} %} + + + +Output: + +.. code-block:: html + + + asset ~~~~~ @@ -157,6 +201,11 @@ csrf_token Renders a CSRF token. Use this function if you want :doc:`CSRF protection ` 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 ~~~~~~~~~~ @@ -532,6 +581,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 ~~~~~~~~~~~ @@ -550,6 +621,36 @@ Generates the file path inside an ```` 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 + + my_text at line 1 + + +Example 2 + +.. code-block:: twig + + {{ "path/to/file/file.txt"|format_file(line = 3) }} + +Output: + +.. code-block:: html + + + file.txt + / at line 3 + + format_file_from_text ~~~~~~~~~~~~~~~~~~~~~ From ead46c557e9d13a48e7f0568cb3eff638d2e972f Mon Sep 17 00:00:00 2001 From: Edgar Brunet Date: Fri, 29 Nov 2024 15:10:26 +0100 Subject: [PATCH 2/4] [Twig] [twig reference] add examples to functions (format_file, file_exercp, fragment_uri, controller, csrf_token) --- reference/twig_reference.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 523a0e87705..7aa3ea92322 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -85,7 +85,7 @@ Example 1 .. code-block:: twig - {{ fragment_uri(controller('App\\Controller\\HomeController::index'), + {{ fragment_uri(controller('App\\Controller\\HomeController::index'), absolute = true, strict = true, sign = true) }} Output: @@ -100,7 +100,7 @@ Example 2 .. code-block:: twig - {{ fragment_uri(controller('App\\Controller\\UserController::profile', + {{ fragment_uri(controller('App\\Controller\\UserController::profile', { 'id': 42 }), absolute = false, strict = false, sign = false) }} Output: @@ -131,7 +131,7 @@ like :ref:`render() ` and .. _reference-twig-function-asset: -.. code-block:: twig +.. code-block:: html+twig {% set myArray = {'a': 'foo', 'b': 'bar'} %} @@ -631,7 +631,7 @@ Output: .. code-block:: html - my_text at line 1 @@ -645,7 +645,7 @@ Output: .. code-block:: html - file.txt / at line 3 From 3dadb0ddf31bfb0590708f4f86fcb3a9f0dc3bbb Mon Sep 17 00:00:00 2001 From: Edgar Brunet Date: Fri, 29 Nov 2024 15:21:59 +0100 Subject: [PATCH 3/4] [Twig] [twig reference] add examples to functions (format_file, file_exercp, fragment_uri, controller, csrf_token) --- reference/twig_reference.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 7aa3ea92322..f416d54f5a3 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -85,8 +85,7 @@ Example 1 .. code-block:: twig - {{ fragment_uri(controller('App\\Controller\\HomeController::index'), - absolute = true, strict = true, sign = true) }} + {{ fragment_uri(controller('App\\Controller\\HomeController::index'), absolute = true, strict = true, sign = true) }} Output: @@ -100,8 +99,7 @@ Example 2 .. code-block:: twig - {{ fragment_uri(controller('App\\Controller\\UserController::profile', - { 'id': 42 }), absolute = false, strict = false, sign = false) }} + {{ fragment_uri(controller('App\\Controller\\UserController::profile', { 'id': 42 }), absolute = false, strict = false, sign = false) }} Output: From b858383de387cd4019ee030a2f74ec9966a39c03 Mon Sep 17 00:00:00 2001 From: Edgar Brunet Date: Fri, 29 Nov 2024 15:24:16 +0100 Subject: [PATCH 4/4] [Twig] [twig reference] add examples to functions (format_file, file_exercp, fragment_uri, controller, csrf_token) --- reference/twig_reference.rst | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index f416d54f5a3..2df159b283a 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -81,33 +81,6 @@ Generates the URI of :ref:`a fragment `. .. versionadded:: 5.3 The ``fragment_uri()`` function was introduced in Symfony 5.3. -Example 1 - -.. code-block:: twig - - {{ fragment_uri(controller('App\\Controller\\HomeController::index'), absolute = true, strict = true, sign = true) }} - -Output: - -.. code-block:: html - - - https://example.com/_fragment?_path=_controller=App%5CController%5CHomeController%3A%3Aindex&_hash=abcdef123456 - - -Example 2 - -.. code-block:: twig - - {{ fragment_uri(controller('App\\Controller\\UserController::profile', { 'id': 42 }), absolute = false, strict = false, sign = false) }} - -Output: - -.. code-block:: html - - - /_fragment?_path=_controller=App%5CController%5CUserController%3A%3Aprofile&id=42 - controller ~~~~~~~~~~