diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-04-23 13:36:26 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-04-23 13:40:39 +0200 |
commit | 4633dd989c58e7707f681c0ffd651c8b5336d5a9 (patch) | |
tree | f96eaa4fb09932c9e8e6be142802270a6b3b1f6c | |
parent | 641940ef3805589e92b4520d4944ed66afc875fb (diff) | |
download | askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.tar.gz askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.tar.bz2 askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.zip |
Fix taking references of function arguments (fixes #311)
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(), ""); +} |