askama icon indicating copy to clipboard operation
askama copied to clipboard

`num as {integer}` cast does not work?

Open realGWM opened this issue 8 months ago • 4 comments

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?

realGWM avatar Oct 17 '23 14:10 realGWM

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

djc avatar Oct 18 '23 12:10 djc

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());
}

realGWM avatar Oct 18 '23 13:10 realGWM

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.

djc avatar Oct 18 '23 13:10 djc

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

Kijewski avatar Oct 18 '23 14:10 Kijewski