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

Parsing error when using closures in template #643

@OskarPersson

Description

@OskarPersson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions