From df54534ae6e7600921df60ed38f3ba65196ba39e Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 1 Jun 2025 11:16:06 -0700 Subject: [PATCH] bugfix: resolve with macro with call after caller https://github.com/askama-rs/askama/issues/467 Signed-off-by: Michael Pollind --- askama_derive/src/generator/node.rs | 2 +- testing/tests/macro.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/askama_derive/src/generator/node.rs b/askama_derive/src/generator/node.rs index a4ccbd12..dcf78540 100644 --- a/askama_derive/src/generator/node.rs +++ b/askama_derive/src/generator/node.rs @@ -779,7 +779,7 @@ impl<'a> Generator<'a, '_> { })?; self.prepare_ws(ws2); self.seen_callers.pop(); - self.active_caller = None; + self.active_caller = self.seen_callers.last().map(|v| v.0); Ok(size_hint) } diff --git a/testing/tests/macro.rs b/testing/tests/macro.rs index f508b407..82284aa6 100644 --- a/testing/tests/macro.rs +++ b/testing/tests/macro.rs @@ -254,6 +254,27 @@ fn test_caller_expr() { assert_eq!(MacroCallerExpr.render().unwrap(), "20 1 35\n"); } +#[test] +fn test_caller_in_macro_call_before_caller() { + #[derive(Template)] + #[template( + source = r#" + {%- macro test2() -%} + a + {{- caller() -}} + {%- endmacro -%} + {%- macro test() -%} + {%- call test2() -%}b{%- endcall -%} + {{- caller() -}} + {%- endmacro -%} + {%- call test() -%}{%- call test2() -%}b{%- endcall -%}{%- endcall -%} + "#, + ext = "txt" + )] + struct CallerWithMacro; + assert_eq!(CallerWithMacro.render().unwrap(), "abab"); +} + #[test] fn test_caller_in_caller() { #[derive(Template)]