diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2021-11-10 19:00:41 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-11-11 15:35:45 +0100 |
commit | f6b79cd82adae8fa598c5d4db6cf41c2781eb214 (patch) | |
tree | 194eb3149a52a1b10ddbb948011fcba0f25385e8 /askama_shared/src/parser.rs | |
parent | 393a0ebc368cdf6b2be1de2097092dd1959bda36 (diff) | |
download | askama-f6b79cd82adae8fa598c5d4db6cf41c2781eb214.tar.gz askama-f6b79cd82adae8fa598c5d4db6cf41c2781eb214.tar.bz2 askama-f6b79cd82adae8fa598c5d4db6cf41c2781eb214.zip |
Implement `for … in … if …`
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/parser.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 90c2fe3..34a391d 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -38,6 +38,7 @@ pub struct Loop<'a> { pub ws1: Ws, pub var: Target<'a>, pub iter: Expr<'a>, + pub cond: Option<Expr<'a>>, pub body: Vec<Node<'a>>, pub ws2: Ws, pub else_block: Vec<Node<'a>>, @@ -914,6 +915,7 @@ fn parse_loop_content<'a>(i: &'a [u8], s: &State<'_>) -> IResult<&'a [u8], Vec<N } fn block_for<'a>(i: &'a [u8], s: &State<'_>) -> IResult<&'a [u8], Node<'a>> { + let if_cond = preceded(ws(tag("if")), cut(ws(expr_any))); let else_block = |i| { let mut p = preceded( ws(tag("else")), @@ -938,6 +940,7 @@ fn block_for<'a>(i: &'a [u8], s: &State<'_>) -> IResult<&'a [u8], Node<'a>> { ws(tag("in")), cut(tuple(( ws(expr_any), + opt(if_cond), opt(char('-')), |i| tag_block_end(i, s), cut(tuple(( @@ -953,7 +956,8 @@ fn block_for<'a>(i: &'a [u8], s: &State<'_>) -> IResult<&'a [u8], Node<'a>> { ))), ))), )); - let (i, (pws1, _, (var, _, (iter, nws1, _, (body, (_, pws2, else_block, _, nws2)))))) = p(i)?; + let (i, (pws1, _, (var, _, (iter, cond, nws1, _, (body, (_, pws2, else_block, _, nws2)))))) = + p(i)?; let (nws3, else_block, pws3) = else_block.unwrap_or_default(); Ok(( i, @@ -961,6 +965,7 @@ fn block_for<'a>(i: &'a [u8], s: &State<'_>) -> IResult<&'a [u8], Node<'a>> { ws1: Ws(pws1.is_some(), nws1.is_some()), var, iter, + cond, body, ws2: Ws(pws2.is_some(), nws3), else_block, |