]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Support assignment blocks using `{% set %}`
authorKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 18:08:48 +0000 (14:08 -0400)
committerKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 18:08:48 +0000 (14:08 -0400)
This adds support for the usage of `{% set %}` where the contents of
the block are assigned to the variable instead of handling that within
the block parameters.

Because Jinja separates the filter from the variable within the
`AssignBlock` node, we have to detect when there is a wrapping filter
and extract it so that it can slot in properly.

new_parser.py

index a8d507122b210822017ba6354d990995bff2cd76..040636d363aca180db3c20a1472814d00de479ff 100644 (file)
@@ -51,6 +51,9 @@ def parse_block_pair(ast):
     if block_name == 'if':\r
         return parse_block_if(ast)\r
 \r
+    if block_name == 'set':\r
+        return parse_block_set(ast)\r
+\r
     if block_name == 'with':\r
         return parse_block_with(ast)\r
 \r
@@ -134,6 +137,21 @@ def parse_block_set(ast):
             parse_variable(assignment['value']),\r
             lineno=lineno_from_parseinfo(ast['parseinfo'])\r
         )\r
+    elif 'start' in ast:\r
+        key = parse_variable(ast['start']['parameters'][0]['value'])\r
+        filter = None\r
+\r
+        if isinstance(key, nodes.Filter):\r
+            filter = key\r
+            key = key.node\r
+            filter.node = None\r
+\r
+        return nodes.AssignBlock(\r
+            key,\r
+            filter,\r
+            parse(ast['contents']),\r
+            lineno=lineno_from_parseinfo(ast['parseinfo'])\r
+        )\r
     return None\r
 \r
 def parse_block_with(ast):\r