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 before endif is rendered after if block #490

Closed
msrd0 opened this issue Jun 8, 2021 · 6 comments
Closed

Whitespace before endif is rendered after if block #490

msrd0 opened this issue Jun 8, 2021 · 6 comments

Comments

@msrd0
Copy link
Contributor

msrd0 commented Jun 8, 2021

Consider the following code:

#[derive(Template)]
#[template(source = "{%for byte in list%}{{byte}}{%if !loop.last%}, {%endif%}{%endfor%}", ext = "txt", print = "code")]
struct TestTemplate<'a> {
	list: &'a [u8]
}

This should print a list of numbers, e.g. 1, 2, 3. However, what it prints instead is 1, 2, 3 , which is not what I want and not what is defined in the template. This is likely due to the whitespace after the , and before the endif is rendered after the if block in the generated rust code:

impl < 'a > ::askama::Template for TestTemplate< 'a > {
    fn render_into(&self, writer: &mut dyn ::std::fmt::Write) -> ::askama::Result<()> {
        for (byte, _loop_item) in ::askama::helpers::TemplateLoop::new((&self.list).into_iter()) {
            write!(
                writer,
                "{expr0}",
                expr0 = &::askama::MarkupDisplay::new_unsafe(&byte, ::askama::Text),
            )?;
            if !_loop_item.last {
                writer.write_str(",")?;
            }
            writer.write_str(" ")?;
        }
        Ok(())
    }
    fn extension(&self) -> Option<&'static str> {
        Some("txt")
    }
    fn size_hint(&self) -> usize {
        0
    }
}

Here's the parsed AST in case it helps:

[Loop(WS(false, false), Name("byte"), Var("list"), [Expr(WS(false, false), Var("byte")), Cond([(WS(false, false), Some(Unary("!", Attr(Var("loop"), "last"))), [Lit("", ",", " ")])], WS(false, false))], WS(false, false))]
@msrd0
Copy link
Contributor Author

msrd0 commented Jun 8, 2021

Workaround is to replace , with string literal {{", "}}

@vallentin
Copy link
Collaborator

Damn, I thought I fixed all these cases :)

@djc
Copy link
Owner

djc commented Jun 9, 2021

Thanks for the minimal reproduction! Can you reproduce this with main? It seems to work for me.

msrd0 added a commit to msrd0/bitmap-font that referenced this issue Jun 9, 2021
@msrd0
Copy link
Contributor Author

msrd0 commented Jun 9, 2021

I can confirm that this problem does not appear on the main branch.

However (off-topic, I can open another issue if you want), I'm rather unhappy about the fact that invoking a function with both a variable and a literal now involves manually calling .borrow() on the literal: msrd0/bitmap-font@f8f66f4#diff-3967ea9f7da3e8d2ecc5179bad2129d3f618d6379fc329161ecf9c2df023e524

@djc
Copy link
Owner

djc commented Jun 9, 2021

Yes, please open a separate issue about that. That does seem like a regression.

@msrd0
Copy link
Contributor Author

msrd0 commented Jun 9, 2021

I opened #492

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

3 participants