From df4220eb88aaec78c41df6d1b27216286ecd9b9e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 7 Jan 2017 22:22:42 +0100 Subject: Support elif blocks in parser and generator --- askama/src/parser.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 4759472..dd7fdd8 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -62,12 +62,20 @@ named!(expr_node, map!( delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")), Node::Expr)); -named!(cond_blocks, do_parse!( +named!(cond_elif<(Option, Nodes)>, do_parse!( + tag_s!("{%") >> + ws!(tag_s!("elif")) >> + cond: ws!(expr_filtered) >> + tag_s!("%}") >> + block: parse_template >> + (Some(cond), block))); + +named!(cond_else, do_parse!( tag_s!("{%") >> ws!(tag_s!("else")) >> tag_s!("%}") >> block: parse_template >> - (vec![(None, block)]))); + (block))); named!(block_if, do_parse!( tag_s!("{%") >> @@ -75,15 +83,17 @@ named!(block_if, do_parse!( cond: ws!(expr_filtered) >> tag_s!("%}") >> block: parse_template >> - rest: opt!(cond_blocks) >> + elifs: many0!(cond_elif) >> + rest: opt!(cond_else) >> tag_s!("{%") >> ws!(tag_s!("endif")) >> tag_s!("%}") >> ({ let mut res = Vec::new(); res.push((Some(cond), block)); - if let Some(blocks) = rest { - res.extend(blocks); + res.extend(elifs); + if let Some(else_block) = rest { + res.push((None, else_block)); } Node::Cond(res) }))); -- cgit