diff options
author | yossyJ <28825627+yossyJ@users.noreply.github.com> | 2019-01-05 20:18:59 +0900 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-08 15:59:45 +0100 |
commit | df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12 (patch) | |
tree | e6619644eec17048143e1bca34ee37dc0701dae8 /askama_derive | |
parent | c5c37f56a1232ff924a6bfda6c1c05e64a6241b1 (diff) | |
download | askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.tar.gz askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.tar.bz2 askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.zip |
Add support for loop.last
Diffstat (limited to 'askama_derive')
-rw-r--r-- | askama_derive/src/generator.rs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 67ec2c2..6c02a26 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -467,11 +467,17 @@ impl<'a> Generator<'a> { let expr_code = self.visit_expr_root(iter); self.write_buf_writable(buf); - buf.write("for (_loop_index, "); + buf.write("for (_loop_index, _loop_last, "); self.visit_target(buf, var); match iter { - Expr::Range(_, _, _) => buf.writeln(&format!(") in ({}).enumerate() {{", expr_code)), - _ => buf.writeln(&format!(") in (&{}).into_iter().enumerate() {{", expr_code)), + Expr::Range(_, _, _) => buf.writeln(&format!( + ") in ::askama::helpers::enumerate({}) {{", + expr_code + )), + _ => buf.writeln(&format!( + ") in ::askama::helpers::enumerate((&{}).into_iter()) {{", + expr_code + )), }; self.handle(ctx, body, buf, AstLevel::Nested); @@ -915,6 +921,9 @@ impl<'a> Generator<'a> { } else if attr == "first" { buf.write("(_loop_index == 0)"); return DisplayWrap::Unwrapped; + } else if attr == "last" { + buf.write("_loop_last"); + return DisplayWrap::Unwrapped; } else { panic!("unknown loop variable"); } |