Skip to content

Commit

Permalink
feat(unless_block): support else
Browse files Browse the repository at this point in the history
fixes #283
  • Loading branch information
Goncalerta committed Dec 1, 2018
1 parent 9235289 commit 8577eb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/tags/if_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,33 @@ pub fn unless_block(
options: &LiquidOptions,
) -> Result<Box<Renderable>> {
let condition = parse_condition(arguments)?;
let if_true = Template::new(tokens.parse_all(options)?);

let mut if_true = Vec::new();
let mut if_false = None;

while let Some(element) = tokens.next()? {
match element {
BlockElement::Tag(mut tag) => match tag.name() {
"else" => {
if_false = Some(tokens.parse_all(options)?);
break;
}
_ => if_true.push(tag.parse(&mut tokens, options)?),
},
element => if_true.push(element.parse(&mut tokens, options)?),
}
}

let if_true = Template::new(if_true);
let if_false = if_false.map(Template::new);

tokens.assert_empty();
Ok(Box::new(Conditional {
tag_name: "unless",
condition,
mode: false,
if_true,
if_false: None,
if_false,
}))
}

Expand Down
1 change: 0 additions & 1 deletion tests/conformance_ruby/tags/unless_else_tag_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn test_unless_in_loop() {
}

#[test]
#[ignore]
fn test_unless_else_in_loop() {
assert_template_result!(" TRUE 2 3 ", "{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}", v!({"choices": [1, nil, false]}));
}

0 comments on commit 8577eb1

Please sign in to comment.