]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Add full support for `{% if %}` block
authorKevin <kevin@kevin-brown.com>
Wed, 13 May 2020 22:48:17 +0000 (18:48 -0400)
committerKevin <kevin@kevin-brown.com>
Wed, 13 May 2020 22:48:17 +0000 (18:48 -0400)
new_parser.py
test_template.jinja

index 7ce63f7bf3582e12d5c21b3ec7c458e2a2ac5faf..d83c0a47ed2a18127d8ee2be7c9b5e2b574e467b 100644 (file)
@@ -242,15 +242,48 @@ def parse_block_from(ast):
 \r
 def parse_block_if(ast):\r
     test = parse_conditional_expression(ast['start']['parameters'][0]['value'])\r
-    body = parse(ast['contents'])\r
+    body = ast['contents']\r
     elif_ = []\r
-    else_ = []\r
+\r
+    else_ = _split_contents_at_block(body, 'else')\r
+\r
+    if else_ is not None:\r
+        body, _, else_ = else_\r
+    else:\r
+        else_ = []\r
+\r
+    elif_contents = _split_contents_at_block(body, 'elif')\r
+\r
+    if elif_contents is not None:\r
+        body, _, _ = elif_contents\r
+\r
+    while elif_contents is not None:\r
+        _, elif_condition, elif_contents = elif_contents\r
+\r
+        elif_parsed = _split_contents_at_block(elif_contents, 'elif')\r
+\r
+        if elif_parsed is not None:\r
+            elif_body, _, _ = elif_parsed\r
+        else:\r
+            elif_body = elif_contents\r
+\r
+        elif_.append(\r
+            nodes.If(\r
+                parse_conditional_expression(elif_condition['block']['parameters'][0]['value']),\r
+                parse(elif_body),\r
+                [],\r
+                [],\r
+                lineno=lineno_from_parseinfo(elif_condition['parseinfo'])\r
+            )\r
+        )\r
+\r
+        elif_contents = elif_parsed\r
 \r
     return nodes.If(\r
         test,\r
-        body,\r
+        parse(body),\r
         elif_,\r
-        else_,\r
+        parse(else_),\r
         lineno=lineno_from_parseinfo(ast['parseinfo'])\r
     )\r
 \r
@@ -711,9 +744,7 @@ def _parse_import_context(block_parameters):
 def _split_contents_at_block(contents, block_name):\r
     for index, expression in enumerate(contents):\r
         if 'block' in expression:\r
-            block = parse_block(expression)\r
-\r
             if expression['block']['name'] == block_name:\r
-                return (contents[:index], block, contents[index + 1:])\r
+                return (contents[:index], expression, contents[index + 1:])\r
 \r
     return None\r
index 8a38f1aa35cfd13cb10c01b443069251b69e8357..16bea774fe5b9f5a7a54d8114222e5515d216eec 100644 (file)
@@ -91,3 +91,10 @@ across lines #}
         <dd>{{ user.description }}</dd>\r
     </dl>\r
 {% endcall %}\r
+{% if kenny.sick %}\r
+    Kenny is sick.\r
+{% elif kenny.dead %}\r
+    You killed Kenny!  You bastard!!!\r
+{% else %}\r
+    Kenny looks okay --- so far\r
+{% endif %}\r