diff options
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/generator.rs | 5 | ||||
-rw-r--r-- | testing/templates/macro-short-circuit.html | 9 | ||||
-rw-r--r-- | testing/tests/macro.rs | 10 | ||||
-rw-r--r-- | testing/tests/operators.rs | 10 |
4 files changed, 32 insertions, 2 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index a37ca7c..3a91e3c 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -717,10 +717,11 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } names.write(arg); - values.write("&"); + values.write("&("); values.write(&self.visit_expr_root(args.get(i).ok_or_else(|| { CompileError::String(format!("macro '{}' takes more than {} arguments", name, i)) })?)?); + values.write(")"); self.locals.insert(arg); } @@ -944,7 +945,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { let expression = match wrapped { Wrapped => expr_buf.buf, Unwrapped => format!( - "::askama::MarkupDisplay::new_unsafe(&{}, {})", + "::askama::MarkupDisplay::new_unsafe(&({}), {})", expr_buf.buf, self.input.escaper ), }; diff --git a/testing/templates/macro-short-circuit.html b/testing/templates/macro-short-circuit.html new file mode 100644 index 0000000..4a19b86 --- /dev/null +++ b/testing/templates/macro-short-circuit.html @@ -0,0 +1,9 @@ +{% macro foo(b) -%} + {{ b }} +{%- endmacro -%} +{% call foo(true) -%} +{% call foo(true && true) -%} +{% call foo(true && true && true) -%} +{% call foo(false) -%} +{% call foo(false || true) -%} +{% call foo(false || false || true) -%} diff --git a/testing/tests/macro.rs b/testing/tests/macro.rs index 51fbcdc..459b1e2 100644 --- a/testing/tests/macro.rs +++ b/testing/tests/macro.rs @@ -43,3 +43,13 @@ fn test_deep_import() { let t = DeepImportTemplate; assert_eq!(t.render().unwrap(), "foo"); } + +#[derive(Template)] +#[template(path = "macro-short-circuit.html")] +struct ShortCircuitTemplate {} + +#[test] +fn test_short_circuit() { + let t = ShortCircuitTemplate {}; + assert_eq!(t.render().unwrap(), "truetruetruefalsetruetrue"); +} diff --git a/testing/tests/operators.rs b/testing/tests/operators.rs index 99a2a4c..05c1dab 100644 --- a/testing/tests/operators.rs +++ b/testing/tests/operators.rs @@ -53,3 +53,13 @@ fn test_ranges() { }; assert_eq!(t.render().unwrap(), "abcd\nbcd\n\na\nab"); } + +#[derive(Template)] +#[template(source = "{{ true && true }}{{ false || true }}", ext = "txt")] +struct ShortCircuitTemplate {} + +#[test] +fn test_short_circuit() { + let t = ShortCircuitTemplate {}; + assert_eq!(t.render().unwrap(), "truetrue"); +} |