Skip to content
Myr

edit block assignment section

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent d6998ab · Browse files · 1 file+13 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+13 added−14 removed

  • other:+13 added−14 removed

1 file+13 added−14 removed

modified:docs/templates.rst+13 added-14 removed
1084 unmodified lines
10851086Block Assignments1087~~~~~~~~~~~~~~~~~10881089.. versionadded:: 2.81089It's possible to use `set` as a block to assign the content of the block to a1090variable. This can be used to create multi-line strings, since Jinja doesn't1091support Python's triple quotes (``"""``, ``'''``).10921091Starting with Jinja 2.8, it's possible to also use block assignments to1092capture the contents of a block into a variable name. This can be useful1093in some situations as an alternative for macros. It can also be used to create1094multiline strings instead of triple quotes (''' and """), which Jinja does not1095support. In that case, instead of using an equals sign and a value, you just1096write the variable name and then everything until ``{% endset %}`` is captured.1093Instead of using an equals sign and a value, you only write the variable name,1094and everything until ``{% endset %}`` is captured.10951098Example::1096.. code-block:: jinja10971098 {% set navigation %}1099 <li><a href="/">Index</a>1100 <li><a href="/downloads">Downloads</a>1101 {% endset %}11021105The `navigation` variable then contains the navigation HTML source.1103Filters applied to the variable name will be applied to the block's content.11041107.. versionchanged:: 2.101105.. code-block:: jinja11061109Starting with Jinja 2.10, the block assignment supports filters.11101111Example::11121107 {% set reply | wordwrap %}1108 You wrote:1109 {{ message }}1110 {% endset %}11111112.. versionadded:: 2.811131114.. versionchanged:: 2.1011151116 Block assignment supports filters.11171118.. _extends:11191120Extends1121~~~~~~~
841 unmodified lines