-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Rust macro has no access to parameters passed into {% macro f(param) %}
#966
Comments
That certainly sounds surprising. Do you have a minimal reproducing example? |
This is the simplest reproducing example I could get: macro_rules! test_macro{
($entity:expr) => {
{
println!("{:?}", &$entity);
}
}
}
#[derive(Template)]
#[template(source = "{{ test_macro!(entity) }}", ext = "txt")]
struct TestTemplate<'a> {
entity: &'a str,
} which results in:
It looks like it works if I call the macro using |
Oh I see the issue. Since it's a macro, we can't know if let entity = entity;
{{ test_macro!(entity) }} |
I assume you meant: {% let entity = entity; %}
{{ test_macro!(entity) }} which seems to work indeed. Would it be possible for Askama to automatically bind all struct fields from the template struct to local variables with the same name so that macros will work automatically? |
Yes I did. ^^'
It'd be problematic for different reasons:
What I suggest is instead to add your use case as an example for macros in the askama book. What do you think? |
Thanks for the feedback! I understand. Feel free to add it to the book! |
Not sure if this is a question, feature request or a bug.
From the documentation it's not clear to me in what scope rust macros are evaluated.
For instance, I have this Askama macro, which calls a Rust macro internally:
In some situations this works perfectly, while in other situations this does not and results in a "cannot find value
entity
in this scope". (entity
being the argument passed to the Askama macro.)It does work for one situation where this macro is called in another template within a loop like this:
{% for entity in entities.values() %}
. In any case, I've the impression that it doesn't pick up the argument passed to the Askama macro.Is that understanding correct? Is there something we can improve here to make this possible?
The text was updated successfully, but these errors were encountered: