Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved template loop generation #401

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions testing/tests/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
djc marked this conversation as resolved.
Show resolved Hide resolved

#[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");
}