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

Cached methods call #667

Closed
imbolc opened this issue Apr 19, 2022 · 1 comment
Closed

Cached methods call #667

imbolc opened this issue Apr 19, 2022 · 1 comment

Comments

@imbolc
Copy link

imbolc commented Apr 19, 2022

It uses value of the first method call instead of calling the method each time. E.g.

use askama::Template;
use rand::prelude::*;

#[derive(Template)]
#[template(
    source = "{{ rnd.gen() }} {{ rnd.gen() }}",
    ext = "txt",
    print = "code"
)]
struct Foo {
    rnd: Rnd,
}

struct Rnd;
impl Rnd {
    fn gen(&self) -> i32 {
        rand::thread_rng().gen()
    }
}

fn main() {
    let foo = Foo { rnd: Rnd {} };
    dbg!(foo.render().unwrap());
}

Generates

"{expr0} {expr0}",
expr0 = &::askama::MarkupDisplay::new_unsafe(&(self.rnd.gen()), ::askama::Text),

As you can see {expr0} is calculated only once.

@vallentin
Copy link
Collaborator

This was added in #154. I will add a fix, that checks what kind of expression it is, to avoid that issue

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

2 participants