Repository: djc/askama
Pull request #403 (2020-12-16 08:05:26)

Bump version for askama_gotham


Patch:
diff --git a/askama_gotham/Cargo.toml b/askama_gotham/Cargo.toml
index 93c49d1..b9bee10 100644
--- a/askama_gotham/Cargo.toml
+++ b/askama_gotham/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "askama_gotham"
-version = "0.10.0"
+version = "0.11.0"
 authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"]
 description = "Gotham integration for Askama templates"
 documentation = "https://docs.rs/askama"
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index 146f9db..0053b38 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -667,8 +667,26 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
                 ", _loop_item) in ::askama::helpers::TemplateLoop::new({}) {{",
                 expr_code
             )),
+            Expr::Array(..) => buf.writeln(&format!(
+                ", _loop_item) in ::askama::helpers::TemplateLoop::new({}.iter()) {{",
+                expr_code
+            )),
+            // If `iter` is a call then we assume it's something that returns
+            // an iterator. If not then the user can explicitly add the needed
+            // call without issues.
+            Expr::MethodCall(..) | Expr::PathCall(..) | Expr::Index(..) => buf.writeln(&format!(
+                ", _loop_item) in ::askama::helpers::TemplateLoop::new(({}).into_iter()) {{",
+                expr_code
+            )),
+            // If accessing `self` then it most likely needs to be
+            // borrowed, to prevent an attempt of moving.
+            _ if expr_code.starts_with("self.") => buf.writeln(&format!(
+                ", _loop_item) in ::askama::helpers::TemplateLoop::new(((&{}).into_iter())) {{",
+                expr_code
+            )),
+            // Otherwise, we borrow `iter` assuming that it implements `IntoIterator`.
             _ => buf.writeln(&format!(
-                ", _loop_item) in ::askama::helpers::TemplateLoop::new((&{}).into_iter()) {{",
+                ", _loop_item) in ::askama::helpers::TemplateLoop::new(({}).into_iter()) {{",
                 expr_code
             )),
         }?;
diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs
index a40e967..a03952d 100644
--- a/askama_shared/src/parser.rs
+++ b/askama_shared/src/parser.rs
@@ -802,7 +802,7 @@ fn block_match<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>
 fn block_let(i: &[u8]) -> IResult<&[u8], Node> {
     let mut p = tuple((
         opt(tag("-")),
-        ws(tag("let")),
+        ws(alt((tag("let"), tag("set")))),
         ws(alt((target_single, target_tuple))),
         opt(tuple((ws(tag("=")), ws(expr_any)))),
         opt(tag("-")),
diff --git a/book/src/template_syntax.md b/book/src/template_syntax.md
index 3f89213..befde8f 100644
--- a/book/src/template_syntax.md
+++ b/book/src/template_syntax.md
@@ -16,7 +16,7 @@ Inside code blocks, you can also declare variables or assign values
 to variables.
 Assignments can't be imported by other templates.
 
-Assignments use the let tag:
+Assignments use the `let` tag:
 
 ```text
 {% let name = user.name %}
@@ -31,6 +31,8 @@ Assignments use the let tag:
 {{ val }}
 ```
 
+For compatibility with Jinja, `set` can be used in place of `let`.
+
 ## Filters
 
 Values such as those obtained from variables can be post-processed


Test patch:
diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs
index 3ce6185..4345b4f 100644
--- a/testing/tests/loops.rs
+++ b/testing/tests/loops.rs
@@ -67,3 +67,39 @@ fn test_for_range() {
         "foo (first)\nfoo (last)\nbar\nbar\nfoo\nbar\nbar\n"
     );
 }
+
+#[derive(Template)]
+#[template(source = "{% for i in [1, 2, 3] %}{{ i }}{% endfor %}", ext = "txt")]
+struct ForArrayTemplate {}
+
+#[test]
+fn test_for_array() {
+    let t = ForArrayTemplate {};
+    assert_eq!(t.render().unwrap(), "123");
+}
+
+#[derive(Template)]
+#[template(
+    source = "{% for i in [1, 2, 3].iter() %}{{ i }}{% endfor %}",
+    ext = "txt"
+)]
+struct ForMethodCallTemplate {}
+
+#[test]
+fn test_for_method_call() {
+    let t = ForMethodCallTemplate {};
+    assert_eq!(t.render().unwrap(), "123");
+}
+
+#[derive(Template)]
+#[template(
+    source = "{% for i in [1, 2, 3, 4, 5][3..] %}{{ i }}{% endfor %}",
+    ext = "txt"
+)]
+struct ForIndexTemplate {}
+
+#[test]
+fn test_for_index() {
+    let t = ForIndexTemplate {};
+    assert_eq!(t.render().unwrap(), "45");
+}