From d6998ab74e628c9851042248c9e10dc25936630b Mon Sep 17 00:00:00 2001 From: ratchek Date: Tue, 12 Dec 2023 20:43:36 -0500 Subject: [PATCH 1/2] Make ease of use update to template documentation Add the phrases 'multiline comment' and 'triple quotes' to docs in the templates/#block-assignments section. This allows for new users to find this alternative easily. --- docs/templates.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/templates.rst b/docs/templates.rst index d5f2719e0..758ba90ce 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1090,9 +1090,10 @@ Block Assignments Starting with Jinja 2.8, it's possible to also use block assignments to capture the contents of a block into a variable name. This can be useful -in some situations as an alternative for macros. In that case, instead of -using an equals sign and a value, you just write the variable name and then -everything until ``{% endset %}`` is captured. +in some situations as an alternative for macros. It can also be used to create +multiline strings instead of triple quotes (''' and """), which Jinja does not +support. In that case, instead of using an equals sign and a value, you just +write the variable name and then everything until ``{% endset %}`` is captured. Example:: From 8a8eafc6b992ba177f1d3dd483f8465f18a11116 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 20 Dec 2024 08:29:04 -0800 Subject: [PATCH 2/2] edit block assignment section --- docs/templates.rst | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/templates.rst b/docs/templates.rst index 758ba90ce..8db8ccaf9 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1086,35 +1086,34 @@ Assignments use the `set` tag and can have multiple targets:: Block Assignments ~~~~~~~~~~~~~~~~~ -.. versionadded:: 2.8 +It's possible to use `set` as a block to assign the content of the block to a +variable. This can be used to create multi-line strings, since Jinja doesn't +support Python's triple quotes (``"""``, ``'''``). -Starting with Jinja 2.8, it's possible to also use block assignments to -capture the contents of a block into a variable name. This can be useful -in some situations as an alternative for macros. It can also be used to create -multiline strings instead of triple quotes (''' and """), which Jinja does not -support. In that case, instead of using an equals sign and a value, you just -write the variable name and then everything until ``{% endset %}`` is captured. +Instead of using an equals sign and a value, you only write the variable name, +and everything until ``{% endset %}`` is captured. -Example:: +.. code-block:: jinja {% set navigation %}
  • Index
  • Downloads {% endset %} -The `navigation` variable then contains the navigation HTML source. - -.. versionchanged:: 2.10 - -Starting with Jinja 2.10, the block assignment supports filters. +Filters applied to the variable name will be applied to the block's content. -Example:: +.. code-block:: jinja {% set reply | wordwrap %} You wrote: {{ message }} {% endset %} +.. versionadded:: 2.8 + +.. versionchanged:: 2.10 + + Block assignment supports filters. .. _extends: