Code Monkey home page Code Monkey logo

Comments (6)

djc avatar djc commented on August 23, 2024

That certainly sounds surprising. Do you have a minimal reproducing example?

from askama.

jonathanslenders avatar jonathanslenders commented on August 23, 2024

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:

error[E0425]: cannot find value `entity` in this scope
  --> src/.../main.rs:29:10
   |
29 | #[derive(Template)]

It looks like it works if I call the macro using self.entity, but this is not what I want, because I'd like to reuse the template in a situation where it's nested in a loop.

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

Oh I see the issue. Since it's a macro, we can't know if entity is from self or just a token (for a name you're generating for example?). I think this code would work in all cases:

let entity = entity;
{{ test_macro!(entity) }}

from askama.

jonathanslenders avatar jonathanslenders commented on August 23, 2024

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?

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

I assume you meant:
...

Yes I did. ^^'

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?

It'd be problematic for different reasons:

  • If you have a big struct, it'll generate a lot of variables that might not be used. Lot of dead code in short, meaning bigger code to compile, meaning longer compile time.
  • It could lead to surprising/unwanted usage of variables. For example you use a variable named x in a template you include. Variable you're supposed to define yourself which you do normally because of the error. But if you type has an x field, then it'll just be a surprising (and likely unwanted) result.

What I suggest is instead to add your use case as an example for macros in the askama book. What do you think?

from askama.

jonathanslenders avatar jonathanslenders commented on August 23, 2024

Thanks for the feedback! I understand. Feel free to add it to the book!

from askama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.