Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Parsing error when using closures in template #643

Open
OskarPersson opened this issue Feb 25, 2022 · 1 comment
Open

Parsing error when using closures in template #643

OskarPersson opened this issue Feb 25, 2022 · 1 comment

Comments

@OskarPersson
Copy link

OskarPersson commented Feb 25, 2022

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
@vallentin
Copy link
Collaborator

Closures are not (yet?) supported by Askama

Related: #611

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants