From 3f8bcd5f0bbb153b4fd9fdb8c346fc04e8b45fe1 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 11 May 2020 22:48:12 -0400 Subject: [PATCH] Add support for parsing `{% include %}` tags This does not yet support the `ignore_missing` flag that can be passed as a parameter to an `{% include %}` tag --- new_parser.py | 22 ++++++++++++++++++++++ test_template.jinja | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/new_parser.py b/new_parser.py index 88cd4a62..f786b9f7 100644 --- a/new_parser.py +++ b/new_parser.py @@ -73,6 +73,9 @@ def parse_block(ast): if block_name == 'from': return parse_block_from(ast) + if block_name == 'include': + return parse_block_include(ast) + if block_name == 'set': return parse_block_set(ast) @@ -220,6 +223,25 @@ def parse_block_if(ast): lineno=lineno_from_parseinfo(ast['parseinfo']) ) +def parse_block_include(ast): + block_parameters = ast['block']['parameters'] + + template = parse_conditional_expression(block_parameters[0]['value']) + with_context = _parse_import_context(block_parameters) + ignore_missing = False + + if with_context is None: + with_context = False + else: + del block_parameters[-2:] + + return nodes.Include( + template, + with_context, + ignore_missing, + lineno=lineno_from_parseinfo(ast['parseinfo']) + ) + def parse_block_macro(ast): definition = parse_variable(ast['start']['parameters'][0]['value']) name = definition.node.name diff --git a/test_template.jinja b/test_template.jinja index f1a76e3a..9651b8e0 100644 --- a/test_template.jinja +++ b/test_template.jinja @@ -71,4 +71,5 @@ across lines #} {{ foo is not bar }} {{ not (foo and bar) }} {{ foo not in bar }} -{% from 'forms.html' import input with context %} \ No newline at end of file +{% from 'forms.html' import input with context %} +{% include 'header.html' without context %} \ No newline at end of file -- 2.47.3