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

num as {integer} cast does not work? #882

Open
realGWM opened this issue Oct 17, 2023 · 4 comments
Open

num as {integer} cast does not work? #882

realGWM opened this issue Oct 17, 2023 · 4 comments

Comments

@realGWM
Copy link

realGWM commented Oct 17, 2023

Hi, I have this MRE:

use askama::Template;

#[derive(Template)]
#[template(ext = "txt", source = r#"
    {% let a = 51 %} // works
    {% let a = 51 as i64 %} // does not work
"#)]
struct HelloTemplate {}

fn main() {
    let a = 51 as i64; // sanity check that it works in normal Rust code
    let hello = HelloTemplate {};
    println!("{}", hello.render().unwrap());
}

and it doesn't compile because of the line with casting as i64.
Is there some alternative syntax for it that I'm missing?

@djc
Copy link
Owner

djc commented Oct 18, 2023

It's possible we don't support as casts. Maybe 51_i64 is supported? I forget...

@realGWM
Copy link
Author

realGWM commented Oct 18, 2023

Nope, doesn't work either :(

use askama::Template;

#[derive(Template)]
#[template(ext = "txt", source = r#"
    {% let a = 51 %} // works
    {% let a = 51_i64 %} // does not work
"#)]
struct HelloTemplate {}

fn main() {
    let a = 51_i64; // sanity check that it works in normal Rust code
    let hello = HelloTemplate {};
    println!("{}", hello.render().unwrap());
}

@djc
Copy link
Owner

djc commented Oct 18, 2023

Okay, well, if you want to try hacking it in, the code for binary expressions is around here:

https://github.com/djc/askama/blob/main/askama_parser/src/expr.rs#L107

If you can submit a patch with a basic test I'm happy to review it and/or provide more guidance.

@Kijewski
Copy link
Collaborator

In the meantime using i64::from(51) should work.

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

3 participants