Add support for loop.last

This commit is contained in:
yossyJ 2019-01-05 20:18:59 +09:00 committed by Dirkjan Ochtman
parent c5c37f56a1
commit df07f7f5e3
7 changed files with 72 additions and 7 deletions

View File

@ -433,6 +433,7 @@ pub trait Template {
}
pub use crate::shared::filters;
pub use crate::shared::helpers;
pub use crate::shared::{read_config_file, Error, MarkupDisplay, Result};
pub use askama_derive::*;

View File

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

View File

@ -0,0 +1,48 @@
use std::iter::Peekable;
pub struct Enumerate<I>
where
I: Iterator,
{
iter: Peekable<I>,
count: usize,
}
impl<I> Iterator for Enumerate<I>
where
I: Iterator,
{
type Item = (usize, bool, <I as Iterator>::Item);
#[inline]
fn next(&mut self) -> Option<(usize, bool, <I as Iterator>::Item)> {
self.iter.next().map(|a| {
let last = self.iter.peek().is_none();
let ret = (self.count, last, a);
// Possible undefined overflow.
self.count += 1;
ret
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn count(self) -> usize {
self.iter.count()
}
}
#[inline]
pub fn enumerate<I>(iter: I) -> Enumerate<I>
where
I: Iterator,
{
Enumerate {
iter: iter.peekable(),
count: 0,
}
}

View File

@ -16,6 +16,7 @@ pub use askama_escape::MarkupDisplay;
use std::collections::BTreeMap;
pub mod filters;
pub mod helpers;
#[derive(Debug)]
pub struct Config<'a> {

View File

@ -1,5 +1,5 @@
{% for s in 0..2 -%}
foo
foo{% if loop.first %} (first){% endif %}{% if loop.last %} (last){% endif %}
{% endfor -%}
{% for s in init..1 -%}

View File

@ -1,3 +1,3 @@
{% for s in strings %}
{{- loop.index0 }}. {{ s }}{{ 2 * loop.index }}{% if !loop.first %}{% else %} (first){% endif %}
{{- loop.index0 }}. {{ s }}{{ 2 * loop.index }}{% if !loop.first %}{% else %} (first){% endif %}{% if loop.last %} (last){% endif %}
{% endfor %}

View File

@ -46,7 +46,10 @@ fn test_precedence_for() {
let s = PrecedenceTemplate {
strings: vec!["A", "alfa", "1"],
};
assert_eq!(s.render().unwrap(), "0. A2 (first)\n1. alfa4\n2. 16\n");
assert_eq!(
s.render().unwrap(),
"0. A2 (first)\n1. alfa4\n2. 16 (last)\n"
);
}
#[derive(Template)]
@ -59,5 +62,8 @@ struct ForRangeTemplate {
#[test]
fn test_for_range() {
let s = ForRangeTemplate { init: -1, end: 1 };
assert_eq!(s.render().unwrap(), "foo\nfoo\nbar\nbar\nfoo\nbar\nbar\n");
assert_eq!(
s.render().unwrap(),
"foo (first)\nfoo (last)\nbar\nbar\nfoo\nbar\nbar\n"
);
}