diff options
-rw-r--r-- | askama_derive/src/generator.rs | 5 | ||||
-rw-r--r-- | testing/templates/for-range.html | 15 | ||||
-rw-r--r-- | testing/tests/loops.rs | 13 |
3 files changed, 32 insertions, 1 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index b6b5146..2a233d5 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -404,7 +404,10 @@ impl<'a> Generator<'a> { self.locals.insert(name); buf.write(name); } - buf.writeln(&format!(") in (&{}).into_iter().enumerate() {{", expr_code)); + match iter { + Expr::Range(_, _, _) => buf.writeln(&format!(") in ({}).enumerate() {{", expr_code)), + _ => buf.writeln(&format!(") in (&{}).into_iter().enumerate() {{", expr_code)), + }; self.handle(ctx, body, buf, AstLevel::Nested); self.handle_ws(buf, ws2); diff --git a/testing/templates/for-range.html b/testing/templates/for-range.html new file mode 100644 index 0000000..9b109bb --- /dev/null +++ b/testing/templates/for-range.html @@ -0,0 +1,15 @@ +{% for s in 0..2 -%} + foo +{% endfor -%} + +{% for s in init..1 -%} + bar +{% endfor -%} + +{% for s in 0..end -%} + foo +{% endfor -%} + +{% for s in init..end -%} + bar +{% endfor -%} diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs index 70ebb21..a0736f5 100644 --- a/testing/tests/loops.rs +++ b/testing/tests/loops.rs @@ -46,3 +46,16 @@ fn test_precedence_for() { }; assert_eq!(s.render().unwrap(), "0. A2 (first)\n1. alfa4\n2. 16\n"); } + +#[derive(Template)] +#[template(path = "for-range.html")] +struct ForRangeTemplate { + init: i32, + end: i32, +} + +#[test] +fn test_for_range() { + let s = ForRangeTemplate { init: -1, end: 1 }; + assert_eq!(s.render().unwrap(), "foo\nfoo\nbar\nbar\nfoo\nbar\nbar\n"); +} |