diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 7d295240..47ec57c7 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -545,6 +545,7 @@ impl<'a> Generator<'a> { } buf.writeln("}")?; + self.locals.pop(); Ok(flushed + ((size_hint1 * 3) + size_hint2) / 2) } diff --git a/testing/templates/if-let-with-for.html b/testing/templates/if-let-with-for.html new file mode 100644 index 00000000..b0de7f98 --- /dev/null +++ b/testing/templates/if-let-with-for.html @@ -0,0 +1,11 @@ +{%- if let Some(thing) = thing -%} + {%- for item in thing.items -%} + {{ item }} + {%- endfor -%} +{%- endif -%} + +{%- if let Some(thing) = thing -%} + {%- for item in thing.items -%} + {{ item }} + {%- endfor -%} +{%- endif -%} diff --git a/testing/tests/if_let.rs b/testing/tests/if_let.rs index d3834181..da5428bf 100644 --- a/testing/tests/if_let.rs +++ b/testing/tests/if_let.rs @@ -128,3 +128,26 @@ fn test_elif() { assert_eq!(Elif { s: None }.render().unwrap(), "empty"); assert_eq!(Elif { s: Some("tada") }.render().unwrap(), "tada"); } + +#[derive(Template)] +#[template(path = "if-let-with-for.html")] +struct IfLetWithForTemplate { + thing: Option, +} + +struct IfLetWithForTemplateThing { + items: Vec, +} + +#[test] +fn test_if_let_with_for() { + let s = IfLetWithForTemplate { + thing: Some(IfLetWithForTemplateThing { + items: vec![1, 2, 3], + }), + }; + assert_eq!(s.render().unwrap(), "123123"); + + let t = IfLetWithForTemplate { thing: None }; + assert_eq!(t.render().unwrap(), ""); +}