askama icon indicating copy to clipboard operation
askama copied to clipboard

Parsing error when using closures in template

Open OskarPersson opened this issue 4 years ago • 1 comments

There seem to be an error with the parsing when writing closures in the templates, for example with the following:

main.rs:

use askama::Template;

#[derive(Template)]
#[template(path = "hello.html")]
struct HelloTemplate {
    names: Vec<String>,
}

fn main() {
    let hello = HelloTemplate {
        names: vec!["foo".to_string(), "bar".to_string()],
    };
    println!("{}", hello.render().unwrap()); // then render it.
}

hello.html:

{% for name in names.iter().map(|n| n.to_uppercase()) %}
    Hello, {{ name }}!
{% endfor %}

I get this error:

error: problems parsing template source at row 1, column 28 near:
       "(|n| n.to_uppercase()) %}\n    Hello, {{ "...
 --> src/main.rs:3:10
  |
3 | #[derive(Template)]
  |          ^^^^^^^^
  |
  = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `render` found for struct `HelloTemplate` in the current scope
  --> src/main.rs:13:26
   |
5  | struct HelloTemplate {
   | -------------------- method `render` not found for this
...
13 |     println!("{}", hello.render().unwrap()); // then render it.
   |                          ^^^^^^ method not found in `HelloTemplate`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `render`, perhaps you need to implement it:
           candidate #1: `Template`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `askama-test` due to 2 previous errors

OskarPersson avatar Feb 25 '22 11:02 OskarPersson

Closures are not (yet?) supported by Askama

Related: #611

vallentin avatar Feb 25 '22 12:02 vallentin