Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DONT MERGE] Always take reference when accessing value passed as argument to a macro #714

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ impl MapChain<'_, &str, LocalMeta> {
let name = normalize_identifier(name);
self.get(&name).map(|meta| match &meta.refs {
Some(expr) => expr.clone(),
None => name.to_string(),
None => format!("&{name}"),
})
}

Expand Down
16 changes: 16 additions & 0 deletions testing/templates/macro-iter.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{%- macro list(list) -%}
<ul>
{%- for item in list %}
<li>{{item}}</li>
{%- endfor %}
</ul>
<ol>
{%- for item in list %}
<li>{{item}}</li>
{%- endfor %}
</ol>
{%- endmacro -%}

{%- for foo in foos %}
{% call list(foo.list) %}
{%- endfor %}
11 changes: 11 additions & 0 deletions testing/tests/macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use askama::Template;
use std::collections::BTreeSet;

#[derive(Template)]
#[template(path = "macro.html")]
Expand Down Expand Up @@ -73,3 +74,13 @@ fn str_cmp() {
let t = StrCmpTemplate;
assert_eq!(t.render().unwrap(), "AfooBotherCneitherD");
}

#[derive(Template)]
#[template(path = "macro-iter.html.j2")]
struct MacroIterTemplate {
foos: Vec<Foo>,
}

struct Foo {
list: BTreeSet<String>,
}