Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block partial rendering #1054

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl<'a> Generator<'a> {
buf.write(CRATE);
buf.writeln("::Result<()> {")?;

buf.discard = self.buf_writable.discard;
// Make sure the compiler understands that the generated code depends on the template files.
for path in self.contexts.keys() {
// Skip the fake path of templates defined in rust source.
Expand All @@ -113,6 +114,7 @@ impl<'a> Generator<'a> {
} else {
self.handle(ctx, ctx.nodes, buf, AstLevel::Top)
}?;
buf.discard = false;

self.flush_ws(Ws(None, None));
buf.write(CRATE);
Expand Down Expand Up @@ -987,6 +989,13 @@ impl<'a> Generator<'a> {
if block_fragment_write {
self.buf_writable.discard = true;
}
// If we are rendering a specific block and the discard changed, it means that we're done
// with the block we want to render and that from this point, everything will be discarded.
//
// To get this block content rendered as well, we need to write to the buffer before then.
if buf.discard != prev_buf_discard {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems surprising to me -- why do we only want to write if buf.discard is not equal to prev_buf_discard vs writing it out if buf.discard is just false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting question. In my previous PR about this, if I wrote too early, we had content from sub-block that was rendered before its parent's, which was problematic. So in this case, if buf.discard != prev_buf_discard, then it's like that the parent is actually not something being displayed and so we should flush everything before the discard is true again.

Does my reasoning sound good to you and do you see potential corner-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thought: do you want me to add a code comment to explain this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be a good improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is too long IMO and still doesn't make it very clear what case this is trying to fix/prevent. I want a comment here in like 4-5 lines that explains conceptually what case it is trying to handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment.

self.write_buf_writable(buf)?;
}
buf.discard = prev_buf_discard;

Ok(size_hint)
Expand Down
18 changes: 18 additions & 0 deletions testing/tests/block_fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,21 @@ fn test_specific_block() {
let t = RenderInPlace { s1 };
assert_eq!(t.render().unwrap(), "\nSection: [abc]\n");
}

#[derive(Template)]
#[template(
source = r#"{% block empty %}
{% endblock %}

{% if let Some(var) = var %}
{{ var }}
{% endif %}"#,
block = "empty",
ext = "txt"
)]
struct Empty {}

#[test]
fn test_render_only_block() {
assert_eq!(Empty {}.render().unwrap(), "\n");
}
Loading