diff options
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/generator.rs | 26 | ||||
-rw-r--r-- | testing/tests/simple.rs | 10 |
2 files changed, 30 insertions, 6 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index d904524..fe02b8a 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -984,13 +984,13 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { if name == "escape" || name == "safe" || name == "e" || name == "json" { buf.write(&format!( - "::askama::filters::{}({}, &", + "::askama::filters::{}({}, ", name, self.input.escaper )); } else if filters::BUILT_IN_FILTERS.contains(&name) { - buf.write(&format!("::askama::filters::{}(&", name)); + buf.write(&format!("::askama::filters::{}(", name)); } else { - buf.write(&format!("filters::{}(&", name)); + buf.write(&format!("filters::{}(", name)); } self._visit_args(buf, args); @@ -1004,7 +1004,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { fn _visit_format_filter(&mut self, buf: &mut Buffer, args: &[Expr]) { buf.write("format!("); - self._visit_args(buf, args); + if let Some(Expr::StrLit(v)) = args.first() { + self.visit_str_lit(buf, v); + if args.len() > 1 { + buf.write(", "); + } + } else { + panic!("invalid expression type for format filter"); + } + self._visit_args(buf, &args[1..]); buf.write(")"); } @@ -1024,9 +1032,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } fn _visit_args(&mut self, buf: &mut Buffer, args: &[Expr]) { + if args.is_empty() { + return; + } + for (i, arg) in args.iter().enumerate() { if i > 0 { buf.write(", &"); + } else { + buf.write("&"); } let scoped = match *arg { @@ -1172,7 +1186,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } buf.write(part); } - buf.write("(&"); + buf.write("("); self._visit_args(buf, args); buf.write(")"); DisplayWrap::Unwrapped @@ -1196,7 +1210,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.write("self."); buf.write(s); } - buf.write(")(&"); + buf.write(")("); self._visit_args(buf, args); buf.write(")"); DisplayWrap::Unwrapped diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 7db8cb4..7241005 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -392,3 +392,13 @@ mod without_import_on_derive { assert_eq!(WithoutImport.render().unwrap(), "foo"); } } + +#[derive(askama::Template)] +#[template(source = "{% let s = String::new() %}{{ s }}", ext = "txt")] +struct DefineStringVar; + +#[test] +fn test_define_string_var() { + let template = DefineStringVar; + assert_eq!(template.render().unwrap(), ""); +} |