]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Initial support for if blocks
authorKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 16:42:22 +0000 (12:42 -0400)
committerKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 16:42:22 +0000 (12:42 -0400)
This currently only supports tests which are a simple condition and
do not span multiple sides of a comparison.

new_parser.py

index 56a7fe445714048b503068f6aeffdea162a1eec9..3bfedb363526aaac960e2f3a90b6e1826c3ec340 100644 (file)
@@ -45,6 +45,9 @@ def parse_block_pair(ast):
     if block_name == 'for':\r
         return parse_block_for(ast)\r
 \r
+    if block_name == 'if':\r
+        return parse_block_if(ast)\r
+\r
     if block_name == 'with':\r
         return parse_block_with(ast)\r
 \r
@@ -100,6 +103,20 @@ def parse_block_from(ast):
 \r
     return from_import\r
 \r
+def parse_block_if(ast):\r
+    test = parse_conditional_expression(ast['start']['parameters'][0]['value'])\r
+    body = parse(ast['contents'])\r
+    elif_ = None\r
+    else_ = None\r
+\r
+    return nodes.If(\r
+        test,\r
+        body,\r
+        elif_,\r
+        else_,\r
+        lineno=lineno_from_parseinfo(ast['parseinfo'])\r
+    )\r
+\r
 def parse_block_with(ast):\r
     with_node = nodes.With(\r
         lineno=lineno_from_parseinfo(ast['parseinfo'])\r
@@ -122,6 +139,12 @@ def parse_block_with(ast):
     return with_node\r
 \r
 def parse_comment(ast):\r
+    return\r
+\r
+def parse_conditional_expression(ast):\r
+    if 'variable' in ast:\r
+        return parse_variable(ast)\r
+\r
     return None\r
 \r
 def parse_literal(ast):\r