diff options
author | Ryan Kelly <ryan@rfk.id.au> | 2021-06-21 16:35:57 +1000 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-06-22 11:41:45 +0200 |
commit | 49252d2457f280026c020d0df46733578eb959a5 (patch) | |
tree | 37a9f8935f8e0c8ce1c83f6d2f7c69b30e89e5f7 | |
parent | b318d7cbcded2c6dfc66bbe19687f1246a9a9eab (diff) | |
download | askama-49252d2457f280026c020d0df46733578eb959a5.tar.gz askama-49252d2457f280026c020d0df46733578eb959a5.tar.bz2 askama-49252d2457f280026c020d0df46733578eb959a5.zip |
Fix code generation for macro calls that store args in variables.
-rw-r--r-- | askama_shared/src/generator.rs | 5 | ||||
-rw-r--r-- | testing/templates/nested-macro-args.html | 9 | ||||
-rw-r--r-- | testing/tests/macro.rs | 10 |
3 files changed, 23 insertions, 1 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 638a25f..b0489ec 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -717,6 +717,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { let mut names = Buffer::new(0); let mut values = Buffer::new(0); + let mut is_first_variable = true; for (i, arg) in def.args.iter().enumerate() { let expr = args.get(i).ok_or_else(|| { CompileError::String(format!("macro '{}' takes more than {} arguments", name, i)) @@ -742,7 +743,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { // multiple times, e.g. in the case of macro // parameters being used multiple times. _ => { - if i > 0 { + if is_first_variable { + is_first_variable = false + } else { names.write(", "); values.write(", "); } diff --git a/testing/templates/nested-macro-args.html b/testing/templates/nested-macro-args.html new file mode 100644 index 0000000..03826f8 --- /dev/null +++ b/testing/templates/nested-macro-args.html @@ -0,0 +1,9 @@ +{%- macro outer(first) -%} +{%- call inner(first, "second") -%} +{%- endmacro -%} + +{%- macro inner(first, second) -%} +{{ first }} {{ second }} +{%- endmacro -%} + +{%- call outer("first") -%} diff --git a/testing/tests/macro.rs b/testing/tests/macro.rs index 459b1e2..7f7e4dc 100644 --- a/testing/tests/macro.rs +++ b/testing/tests/macro.rs @@ -53,3 +53,13 @@ fn test_short_circuit() { let t = ShortCircuitTemplate {}; assert_eq!(t.render().unwrap(), "truetruetruefalsetruetrue"); } + +#[derive(Template)] +#[template(path = "nested-macro-args.html")] +struct NestedMacroArgsTemplate {} + +#[test] +fn test_nested_macro_with_args() { + let t = NestedMacroArgsTemplate {}; + assert_eq!(t.render().unwrap(), "first second"); +} |