askama icon indicating copy to clipboard operation
askama copied to clipboard

Pass variables to includes

Open Exegetech opened this issue 5 years ago • 3 comments

Is there a way to pass parameters to the includes?

Something like

// page.rs
#[derive(Template)]
#[template(path = "includes/widget.html")]
struct Widget {
  a: String,
  b: String,
}

#[derive(Template)]
#[template(path = "page.html")]
struct Page {
   a: String,
   widget: Widget,
}
// page.html
<span>{{ a }}</span>
{% includes includes/widget.html {{widget}} %}
// includes/widget.html
<span>{{ a }}</span>
<span>{{ b }}</span>

Exegetech avatar May 27 '19 01:05 Exegetech

No; the include should just have access to its including scope. I think typically you would solve this by importing a macro definition from a particular file.

djc avatar May 27 '19 07:05 djc

@djc Can you give me an example on importing that macro definition from a particular file?

Exegetech avatar May 27 '19 14:05 Exegetech

Have a look at the test: https://github.com/djc/askama/blob/master/testing/tests/macro.rs#L16

djc avatar May 28 '19 08:05 djc