Skip to content

Commit

Permalink
Fix include statements within block fragments (#1093)
Browse files Browse the repository at this point in the history
This does not render the include block when used as a fragment,
causing the test to fail.

Co-authored-by: Oscar Giles <[email protected]>
  • Loading branch information
mlabrenz117 and OscartGiles authored Sep 11, 2024
1 parent 668bd6f commit 96a4a46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ impl<'a> Generator<'a> {
};
let locals = MapChain::with_parent(&self.locals);
let mut child = Self::new(self.input, self.contexts, heritage.as_ref(), locals);
child.buf_writable.discard = self.buf_writable.discard;
let mut size_hint = child.handle(handle_ctx, handle_ctx.nodes, buf, AstLevel::Top)?;
size_hint += child.write_buf_writable(buf)?;
self.prepare_ws(i.ws);
Expand Down
9 changes: 9 additions & 0 deletions testing/templates/fragment-include.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "fragment-base.html" %}

{% block body %}
{% include "included.html" %}
{% endblock %}

{% block other_body %}
<p>Don't render me.</p>
{% endblock %}
12 changes: 12 additions & 0 deletions testing/tests/block_fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ fn test_specific_block() {
let t = RenderInPlace { s1 };
assert_eq!(t.render().unwrap(), "\nSection: [abc]\n");
}

#[derive(Template)]
#[template(path = "fragment-include.html", block = "body")]
struct FragmentInclude<'a> {
s: &'a str,
}

#[test]
fn test_fragment_include() {
let fragment_include = FragmentInclude { s: "world" };
assert_eq!(fragment_include.render().unwrap(), "\nINCLUDED: world\n");
}

0 comments on commit 96a4a46

Please sign in to comment.