diff --git a/.gitignore b/.gitignore index a6618e1..321e809 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ pip-log.txt # Editor files .idea + +# Virtual environment +.venv \ No newline at end of file diff --git a/render_block/django.py b/render_block/django.py index 977ac7d..3506464 100644 --- a/render_block/django.py +++ b/render_block/django.py @@ -33,9 +33,12 @@ def django_render_block(template, block_name, context, request=None): # Bind the template to the context. with context_instance.bind_template(template): - # Before trying to render the template, we need to traverse the tree of - # parent templates and find all blocks in them. - parent_template = _build_block_context(template, context_instance) + parent_templates = [] + + # Populates parent_templates with the parent templates of the current + # template, and adds the blocks from those parent templates to the + # context. + _build_block_context(template, context_instance) try: return _render_template_block(template, block_name, context_instance) @@ -43,14 +46,20 @@ def django_render_block(template, block_name, context, request=None): # The block wasn't found in the current template. # If there's no parent template (i.e. no ExtendsNode), re-raise. - if not parent_template: + if not len(parent_templates): raise # Check the parent template for this block. - return _render_template_block(parent_template, block_name, context_instance) + for parent_template in parent_templates: + try: + return _render_template_block( + parent_template, block_name, context_instance + ) + except BlockNotFound: + continue -def _build_block_context(template, context): +def _build_block_context(template, context, parent_templates): """Populate the block context with BlockNodes from parent templates.""" # Ensure there's a BlockContext before rendering. This allows blocks in @@ -74,9 +83,9 @@ def _build_block_context(template, context): } ) - _build_block_context(compiled_parent, context) - return compiled_parent - + _build_block_context(compiled_parent, context, parent_templates) + parent_templates.insert(0, compiled_parent) + return parent_templates # The ExtendsNode has to be the first non-text node. if not isinstance(node, TextNode): break diff --git a/tests/templates/test7_django.html b/tests/templates/test7_django.html new file mode 100644 index 0000000..9518ed9 --- /dev/null +++ b/tests/templates/test7_django.html @@ -0,0 +1 @@ +{% block block1 %}block1 from test7{% endblock %} \ No newline at end of file diff --git a/tests/templates/test8_django.html b/tests/templates/test8_django.html new file mode 100644 index 0000000..4edc05b --- /dev/null +++ b/tests/templates/test8_django.html @@ -0,0 +1 @@ +{% extends 'test7_django.html' %} diff --git a/tests/templates/test9_django.html b/tests/templates/test9_django.html new file mode 100644 index 0000000..07ede66 --- /dev/null +++ b/tests/templates/test9_django.html @@ -0,0 +1 @@ +{% extends "test8_django.html" %} \ No newline at end of file diff --git a/tests/tests.py b/tests/tests.py index fc0c2c5..615f04c 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -91,6 +91,10 @@ def test_subblock_no_parent(self): result = render_block_to_string("test_sub.html", "first") self.assertEqual(result, "\nbar\n") + def test_multi_level_inherited_template_block(self): + result = render_block_to_string("test9_django.html", "block1") + self.assertEqual(result, "block1 from test7") + def test_context(self): """Test that a context is properly rendered in a template.""" data = "block2 from test5"