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

rendering nested block broken by if-else-endif in outer block #1075

Open
clonejo opened this issue Jul 6, 2024 · 8 comments
Open

rendering nested block broken by if-else-endif in outer block #1075

clonejo opened this issue Jul 6, 2024 · 8 comments

Comments

@clonejo
Copy link

clonejo commented Jul 6, 2024

Description

When rendering a nested block as a fragment, an if-else-endif clause in the outer block causes this error message:

error: dedent() called while indentation == 0
  --> testing/tests/block_fragments.rs:35:10
   |
35 | #[derive(Template)]
   |          ^^^^^^^^
   |
   = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `askama_testing` (test "block_fragments") due to 1 previous error

It does not occur when the same template is rendered as a whole. It does not occur if only a if-endif clause is used.

Environment

askama main from Git (382b5f6)

How to reproduce

I created a small patch to the test suite: clonejo@96e0fb0

testing/templates/fragment-nested-block.html:

{% extends "fragment-base.html" %}

{% block body %}
<p>Don't render me!</p>
{% if true %}
true
{% else %}
false
{% endif %}
{% block nested %}
<p>I should be here.</p>
{% endblock %}
{% endblock %}

{% block other_body %}
<p>Don't render me!</p>
{% endblock %}
@djc
Copy link
Owner

djc commented Jul 8, 2024

Can you check if #1054 fixes this problem for you?

@clonejo
Copy link
Author

clonejo commented Jul 11, 2024

@djc i tried my test suite patch on top of e44b0ca (from #1054), and i still get the exact same error message.

@clonejo
Copy link
Author

clonejo commented Jul 14, 2024

Looks like this also triggers the same bug with just if-endif twice in the same row:

{% extends "fragment-base.html" %}

{% block body %}
<p>Don't render me!</p>
{%- if true -%}
true
{%- endif -%}{%- if false -%}
false
{%- endif -%}
{% block nested %}
<p>I should be here.</p>
{% endblock %}
{% endblock %}

{% block other_body %}
<p>Don't render me!</p>
{% endblock %}

@djc
Copy link
Owner

djc commented Jul 23, 2024

Would you mind submitting a PR adding this as an integration test? I plan to carve out some time to work on this soon.

@clonejo
Copy link
Author

clonejo commented Aug 3, 2024

Somehow i could not reproduce the bug with endif-if on the same row on current master (5a83872), and even not on 382b5f6.

The reproducer for the original issue is up at #1080.

@djc
Copy link
Owner

djc commented Aug 3, 2024

Thanks for all your efforts, will try to look into this issue soon!

(Currently on vacation so might take me a few weeks.)

@clonejo
Copy link
Author

clonejo commented Aug 3, 2024

Enjoy your vacation, open source can wait :)

@clonejo
Copy link
Author

clonejo commented Aug 3, 2024

I have been digging at the code a bit. What may contribute here is that in some cases, Buffer::writeln() handles indent/dedent, and sometimes the code calling writeln does. I think the code would be easier to reason about if it was only one of the two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants