From 780c93c3f167ca3df26229024e42843358b916ad Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 12 Jan 2017 19:21:15 +0100 Subject: Use 'else if' instead of 'elif' to align more with Rust --- askama/src/parser.rs | 20 +++++++------------- testing/templates/elif.html | 1 - testing/templates/else-if.html | 1 + testing/tests/simple.rs | 8 ++++---- 4 files changed, 12 insertions(+), 18 deletions(-) delete mode 100644 testing/templates/elif.html create mode 100644 testing/templates/else-if.html diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 89fccff..96f123c 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -62,20 +62,18 @@ named!(expr_node, map!( delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")), Node::Expr)); -named!(cond_elif<(Option, Nodes)>, do_parse!( - tag_s!("{%") >> - ws!(tag_s!("elif")) >> +named!(cond_if, do_parse!( + ws!(tag_s!("if")) >> cond: ws!(expr_filtered) >> - tag_s!("%}") >> - block: parse_template >> - (Some(cond), block))); + (cond))); -named!(cond_else, do_parse!( +named!(cond_block<(Option, Nodes)>, do_parse!( tag_s!("{%") >> ws!(tag_s!("else")) >> + cond: opt!(cond_if) >> tag_s!("%}") >> block: parse_template >> - (block))); + (cond, block))); named!(block_if, do_parse!( tag_s!("{%") >> @@ -83,8 +81,7 @@ named!(block_if, do_parse!( cond: ws!(expr_filtered) >> tag_s!("%}") >> block: parse_template >> - elifs: many0!(cond_elif) >> - rest: opt!(cond_else) >> + elifs: many0!(cond_block) >> tag_s!("{%") >> ws!(tag_s!("endif")) >> tag_s!("%}") >> @@ -92,9 +89,6 @@ named!(block_if, do_parse!( let mut res = Vec::new(); res.push((Some(cond), block)); res.extend(elifs); - if let Some(else_block) = rest { - res.push((None, else_block)); - } Node::Cond(res) }))); diff --git a/testing/templates/elif.html b/testing/templates/elif.html deleted file mode 100644 index ad0cbae..0000000 --- a/testing/templates/elif.html +++ /dev/null @@ -1 +0,0 @@ -{% if cond %}true{% elif check %}checked{% else %}false{% endif %} diff --git a/testing/templates/else-if.html b/testing/templates/else-if.html new file mode 100644 index 0000000..469e08e --- /dev/null +++ b/testing/templates/else-if.html @@ -0,0 +1 @@ +{% if cond %}true{% else if check %}checked{% else %}false{% endif %} diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 61f5fa4..697dbd2 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -55,14 +55,14 @@ fn test_else() { #[derive(Template)] -#[template(path = "elif.html")] -struct ElIfTemplate { +#[template(path = "else-if.html")] +struct ElseIfTemplate { cond: bool, check: bool, } #[test] -fn test_elif() { - let s = ElIfTemplate { cond: false, check: true }; +fn test_else_if() { + let s = ElseIfTemplate { cond: false, check: true }; assert_eq!(s.render(), "checked\n"); } -- cgit