Skip to content

Commit

Permalink
Fix wrong handling of nested templates with block
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored and djc committed May 16, 2024
1 parent 2b500b8 commit dd6b6be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
22 changes: 11 additions & 11 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl<'a> Generator<'a> {
}
};

mem::drop(mem::replace(&mut self.buf_writable.buf, current_buf));
self.buf_writable.buf = current_buf;

let mut filter_buf = Buffer::new(buf.indent);
let Filter {
Expand Down Expand Up @@ -904,16 +904,6 @@ impl<'a> Generator<'a> {
name: Option<&'a str>,
outer: Ws,
) -> Result<usize, CompileError> {
let block_fragment_write = self.input.block == name && self.buf_writable.discard;

// Allow writing to the buffer if we're in the block fragment
let mut prev_buf_discard = buf.discard;
if block_fragment_write {
self.buf_writable.discard = false;
} else if self.buf_writable.discard {
prev_buf_discard = mem::replace(&mut buf.discard, true);
}

// Flush preceding whitespace according to the outer WS spec
self.flush_ws(outer);

Expand All @@ -932,6 +922,15 @@ impl<'a> Generator<'a> {
(None, None) => return Err("cannot call 'super()' outside block".into()),
};

self.write_buf_writable(buf)?;

let block_fragment_write = self.input.block == name && self.buf_writable.discard;
// Allow writing to the buffer if we're in the block fragment
if block_fragment_write {
self.buf_writable.discard = false;
}
let prev_buf_discard = mem::replace(&mut buf.discard, self.buf_writable.discard);

// Get the block definition from the heritage chain
let heritage = self
.heritage
Expand Down Expand Up @@ -976,6 +975,7 @@ impl<'a> Generator<'a> {
// Need to flush the buffer before popping the variable stack
child.write_buf_writable(buf)?;
}

child.flush_ws(def.ws2);
self.buf_writable = child.buf_writable;

Expand Down
11 changes: 11 additions & 0 deletions testing/templates/blocks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% block index %}
Section: {{ s1 }}
{% endblock %}

{% block section -%}
[
{%- for value in values -%}
{{ value }}
{%- endfor -%}
]
{%- endblock %}
32 changes: 27 additions & 5 deletions testing/tests/block_fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct FragmentSimple<'a> {
fn test_fragment_simple() {
let simple = FragmentSimple { name: "world" };

assert_eq!(simple.render().unwrap(), "\n\n<p>Hello world!</p>\n");
assert_eq!(simple.render().unwrap(), "\n<p>Hello world!</p>\n");
}

#[derive(Template)]
Expand All @@ -28,7 +28,7 @@ fn test_fragment_super() {

assert_eq!(
sup.render().unwrap(),
"\n\n<p>Hello world!</p>\n\n<p>Parent body content</p>\n\n"
"\n<p>Hello world!</p>\n\n<p>Parent body content</p>\n\n"
);
}

Expand All @@ -43,7 +43,7 @@ fn test_fragment_nested_block() {

assert_eq!(
nested_block.render().unwrap(),
"\n\n<p>I should be here.</p>\n"
"\n<p>I should be here.</p>\n"
);
}

Expand All @@ -61,7 +61,7 @@ fn test_fragment_nested_super() {

assert_eq!(
nested_sup.render().unwrap(),
"\n\n<p>Hello world!</p>\n\n[\n<p>Parent body content</p>\n]\n\n"
"\n<p>Hello world!</p>\n\n[\n<p>Parent body content</p>\n]\n\n"
);
}

Expand All @@ -79,5 +79,27 @@ fn test_fragment_unused_expression() {
required: "Required",
};

assert_eq!(unused_expr.render().unwrap(), "\n\n<p>Required</p>\n");
assert_eq!(unused_expr.render().unwrap(), "\n<p>Required</p>\n");
}

#[derive(Template)]
#[template(path = "blocks.txt", block = "index")]
struct RenderInPlace<'a> {
s1: Section<'a>,
}

#[derive(Template)]
#[template(path = "blocks.txt", block = "section")]
struct Section<'a> {
values: &'a [&'a str],
}

#[test]
fn test_specific_block() {
let s1 = Section {
values: &["a", "b", "c"],
};
assert_eq!(s1.render().unwrap(), "[abc]");
let t = RenderInPlace { s1 };
assert_eq!(t.render().unwrap(), "\nSection: [abc]\n");
}

0 comments on commit dd6b6be

Please sign in to comment.