aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src
diff options
context:
space:
mode:
authorLibravatar vallentin <mail@vallentin.dev>2020-12-15 07:12:24 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-12-16 09:08:12 +0100
commit230263711e7edb0110e1679f6d31353ba7cdc919 (patch)
tree5bbbf8a3a26746c4dfbf5af9c7f28d74dcd69d85 /askama_shared/src
parent5057b75e7752f2186157fb3e1f5e5d2d0c5ff880 (diff)
downloadaskama-230263711e7edb0110e1679f6d31353ba7cdc919.tar.gz
askama-230263711e7edb0110e1679f6d31353ba7cdc919.tar.bz2
askama-230263711e7edb0110e1679f6d31353ba7cdc919.zip
Improved template loop generation (fixes #107, #333)
Diffstat (limited to '')
-rw-r--r--askama_shared/src/generator.rs20
1 files changed, 19 insertions, 1 deletions
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
)),
}?;