Skip to content

Correctly handle local variables in extends blocks#670

Merged
GuillaumeGomez merged 2 commits intoaskama-rs:mainfrom
GuillaumeGomez:variables-in-extends-blocks
Jan 27, 2026
Merged

Correctly handle local variables in extends blocks#670
GuillaumeGomez merged 2 commits intoaskama-rs:mainfrom
GuillaumeGomez:variables-in-extends-blocks

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Fixes #667.

Except for the block blocks, if there is an extends, we don't render anything else. However, we're still supposed to generate variables and function calls and so on. This PR fixes it by first generating the template in "extends mode", so we generate variables, make the function calls and everything. And then, we generate the blocks content.

@Kijewski I'd really like to get your input on this PR to ensure I didn't miss something. However please don't push yourself: if you don't have time or/and motivation or/and energy, I'll simply merge it in a week (so on the 27th) and we can always fix potential new issues later on.

To help you reproduce the original jinja implementation, here's the python script:

# You need to install `jinja2`.
from jinja2 import Environment, FileSystemLoader

# The template are stored in the `templates` folder, like `askama` to make it easier for me to test.
env = Environment(loader=FileSystemLoader("templates/"))
template = env.get_template("child.html")
# `value` is a variable sent to the template, not used here but in case you want to...
print(template.render(value=""))

}

#[derive(Default, Clone, Copy, PartialEq)]
enum RenderFor {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not a big fan of the enum's name nor its variants name. If you have better ideas, I'd gladly take them. Also just realized there is no doc, which is unacceptable. Adding that. :)

@GuillaumeGomez GuillaumeGomez force-pushed the variables-in-extends-blocks branch from 476458a to 01ce7e7 Compare January 20, 2026 16:58
@Kijewski Kijewski self-requested a review January 23, 2026 10:15
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

27th it is, so merging. Gonna do a release as well. Input is still very welcome if issues are found afterward though. :)

@GuillaumeGomez GuillaumeGomez merged commit 3081974 into askama-rs:main Jan 27, 2026
50 checks passed
@GuillaumeGomez GuillaumeGomez deleted the variables-in-extends-blocks branch January 27, 2026 15:00
@Kijewski
Copy link
Copy Markdown
Member

Turns our that one cannot approve a PR after it was already merged. :D The change looks good, approved, thanks! 👍

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Thanks for the check!

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.

Can't access child's root level defined variables inside an inherited block

2 participants