diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-07 22:22:42 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-07 22:22:42 +0100 |
commit | df4220eb88aaec78c41df6d1b27216286ecd9b9e (patch) | |
tree | f1b4f7502e087e6d484269a6749ea3b643b4fd89 | |
parent | c98297fe995d3a2e2d338208eb745b90533fdb3d (diff) | |
download | askama-df4220eb88aaec78c41df6d1b27216286ecd9b9e.tar.gz askama-df4220eb88aaec78c41df6d1b27216286ecd9b9e.tar.bz2 askama-df4220eb88aaec78c41df6d1b27216286ecd9b9e.zip |
Support elif blocks in parser and generator
-rw-r--r-- | askama/src/parser.rs | 20 |
1 files 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<Node>, map!( delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")), Node::Expr)); -named!(cond_blocks<Conds>, do_parse!( +named!(cond_elif<(Option<Expr>, Nodes)>, do_parse!( + tag_s!("{%") >> + ws!(tag_s!("elif")) >> + cond: ws!(expr_filtered) >> + tag_s!("%}") >> + block: parse_template >> + (Some(cond), block))); + +named!(cond_else<Nodes>, do_parse!( tag_s!("{%") >> ws!(tag_s!("else")) >> tag_s!("%}") >> block: parse_template >> - (vec![(None, block)]))); + (block))); named!(block_if<Node>, do_parse!( tag_s!("{%") >> @@ -75,15 +83,17 @@ named!(block_if<Node>, 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) }))); |