Skip to content

Commit 43bfe52

Browse files
committed
Fix includes that use a block fragment
1 parent 2cf8409 commit 43bfe52

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

src/block_fragments/template.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def __init__(self, template, block_name):
2222
self.source = template.source
2323

2424
def render(self, context):
25+
"Display stage -- can be called many times"
26+
2527
# Make a copy of the context and reset the rendering state.
2628
# Trying to re-use a RenderContext in multiple renders can
2729
# lead to TemplateNotFound errors, as Django will skip past
@@ -30,16 +32,20 @@ def render(self, context):
3032
context_instance = copy(context)
3133
context_instance.render_context = RenderContext()
3234

33-
# Bind the template to the context.
34-
with context_instance.render_context.push_state(self.template):
35-
with context_instance.bind_template(self.template):
36-
context.template_name = self.name
35+
with context_instance.render_context.push_state(self):
36+
if context_instance.template is None:
37+
with context_instance.bind_template(self):
38+
context.template_name = self.name
39+
return self._render(context_instance)
40+
else:
41+
return self._render(context_instance)
3742

38-
# Before trying to render the template, we need to traverse the tree of
39-
# parent templates and find all blocks in them.
40-
self._build_block_context(self.template, context_instance)
43+
def _render(self, context):
44+
# Before trying to render the template, we need to traverse the tree of
45+
# parent templates and find all blocks in them.
46+
self._build_block_context(self.template, context)
4147

42-
return self._render_template_block(context_instance)
48+
return self._render_template_block(context)
4349

4450
def _build_block_context(self, template: Template, context: Context) -> None:
4551
"""

tests/templates/test7.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include "test1.html#block2" %}

tests/templates/test8.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% include "test1.html#block1" %}
2+
{% include "test4.html#block1" %}

tests/templates/test9.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% block block1 %}
2+
{% include "test1.html#block2" %}
3+
block1 from test9
4+
{% endblock %}

tests/tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,26 @@ def test_context_autoescape_off(engine):
165165
template.backend.engine.autoescape = autoescape
166166

167167

168-
def test_request_context(engine, settings, rf):
168+
def test_request_context(engine, rf):
169169
"""Test that a request context data are properly rendered in a template."""
170170
request = rf.get("/dummy-url")
171171
template = engine.get_template("test_request_context.html#block1")
172172
assert template.render({"request": request}) == "/dummy-url"
173+
174+
175+
def test_include_fragment(engine):
176+
"""Test that an include tag works with block fragments."""
177+
template = engine.get_template("test7.html")
178+
assert template.render() == "block2 from test1"
179+
180+
181+
def test_multiple_include_fragment(engine):
182+
"""Test multiple include tags that use block fragments."""
183+
template = engine.get_template("test8.html")
184+
assert template.render() == "block1 from test1\nblock1 from test2\n"
185+
186+
187+
def test_include_fragment_in_block(engine):
188+
"""Test a block that does include a fragment."""
189+
template = engine.get_template("test9.html#block1")
190+
assert template.render() == "\nblock2 from test1\nblock1 from test9\n"

0 commit comments

Comments
 (0)