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

Whitespace issue with if conditions #377

Closed
ajeetdsouza opened this issue Nov 6, 2020 · 3 comments · Fixed by #394
Closed

Whitespace issue with if conditions #377

ajeetdsouza opened this issue Nov 6, 2020 · 3 comments · Fixed by #394

Comments

@ajeetdsouza
Copy link

Askama seems to be including whitespace from inside an if condition even when the condition is not fulfilled. Here is my template file:

{%- if some_condition -%}
# WARNING

{% endif -%}

# FILE START

What I expected is:

  • When some_condition is true:

    # WARNING
    
    # FILE START
    
  • When some_condition is false:

    # FILE START
    

What I got is:

  • When some_condition is true: same as expected output

  • When some_condition is false (notice the blank lines before FILE START):

    
    
    # FILE START
    

Is this a bug? If not, is there a way to get the behaviour I described?

@djc
Copy link
Collaborator

djc commented Nov 6, 2020

It sure sounds like a bug! Let me look at some code.

@djc
Copy link
Collaborator

djc commented Nov 6, 2020

So the code generation for if statements is here:

https://github.com/djc/askama/blob/main/askama_shared/src/generator.rs#L502

I don't immediately see anything obviously wrong. Maybe you can contribute a test case and/or use the debugging facility (add print = "code" to your template attribute) to look at the generated code and see what's going on?

@ajeetdsouza
Copy link
Author

@djc I've added the test. Using print = "code" makes the bug more apparent - it's adding the newlines outside the if condition rather than inside:

fn render_into(&self, writer: &mut dyn ::std::fmt::Write) -> ::askama::Result<()> {
    include_bytes !
("/home/ajeetdsouza/ws/askama/testing/templates/cond-whitespace.txt") ;
    if self.show_preamble {
        writer.write_str("preamble")?;
    }
    writer.write_str("\n\nintroduction\n\nconclusion")?;
    Ok(())
}

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

Successfully merging a pull request may close this issue.

2 participants