Skip to content

Add support for let blocks#697

Merged
GuillaumeGomez merged 3 commits intoaskama-rs:mainfrom
GuillaumeGomez:let-blocks
Feb 17, 2026
Merged

Add support for let blocks#697
GuillaumeGomez merged 3 commits intoaskama-rs:mainfrom
GuillaumeGomez:let-blocks

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Fixes #511.

Comment thread askama_derive/src/generator/node.rs Fixed
Comment thread askama_derive/src/generator.rs Dismissed
@Kijewski
Copy link
Copy Markdown
Member

I tried to simplify the implementation a bit by shadowing __askama_writer in let blocks, and not changing anything else compared to d934065, but the whitespace handling does not work. I have no idea why. Can you have a look if you think the idea could work in principle: d934065...fedd29a.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Oh nice! I'll try to take a look this evening.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

It's not only the whitespace handling from what I can see. For example:

#[derive(Template)]
#[template(source = r#"aa {%- set navigation -%}
{{b}}: c {%- endset -%}
{{ navigation }}"#, ext = "txt")]
struct Foo {
    b: u32,
}

fn main() {
    assert_eq!(Foo { b: 0 }.render().unwrap(), "0: c");
}

Outputs aa0 and not aa0: c. The expanded code looks like this:

            __askama_writer.write_str("aa")?;
            let navigation = {
                let mut __askama_writer = askama::helpers::alloc::string::String::new();
                let _ = __askama_writer.try_reserve(6usize);
                {
                    let __askama_writer = &mut __askama_writer;
                    match (
                        &((&&askama::filters::AutoEscaper::new(
                            &(self.b),
                            askama::filters::Text,
                        ))
                            .askama_auto_escape()?),
                    ) {
                        (__askama_expr0,) => {
                            (&&&askama::filters::Writable(__askama_expr0))
                                .askama_write(__askama_writer, __askama_values)?;
                        }
                    }
                }
                __askama_writer
            }

There no : c string anywhere. Seems like some instructions have been skipped.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Fixed merge conflicts while I was at it.

@Kijewski: I'm not sure if it's possible to not have a sub-level (doesn't need to be a full closure like now though). However your proposal is very incomplete for now as it skips big chunks of the let block. Which is good for performance but not if you want the full let block (sorry, I laughed while thinking that so I wrote it 🤣 There is nothing more behind it, I'm curious about your experiment if it can lead to a better generated code that what I produce with this PR).

Copy link
Copy Markdown
Member

@Kijewski Kijewski left a comment

Choose a reason for hiding this comment

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

For now, the change obviously does what it is supposed to do. 👍

If I find the time, I'll have a look how handle_ws() + write_buf_writable() actually work, because I'm not sure I understand what is going on. Whenever I try to use them, it does not work. But if copy and alter existing code, then it does. Spooky. :)

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Dark magic. =D

Well, merging for now. Can be improved in follow-ups.

@GuillaumeGomez GuillaumeGomez merged commit 313f2ca into askama-rs:main Feb 17, 2026
50 checks passed
@GuillaumeGomez GuillaumeGomez deleted the let-blocks branch February 17, 2026 10:47
@GuillaumeGomez GuillaumeGomez mentioned this pull request Apr 5, 2026
12 tasks
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 this pull request may close these issues.

Support for jinja's block assignments

3 participants