Skip to content

Commit

Permalink
Improved template loop generation (fixes #107, #333)
Browse files Browse the repository at this point in the history
  • Loading branch information
vallentin committed Dec 15, 2020
1 parent 3b57663 commit 7c1fec3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion askama_shared/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)),
}?;
Expand Down

0 comments on commit 7c1fec3

Please sign in to comment.