Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not execute elif statements if another statement has already been executed #268

Merged
merged 5 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/IfTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
if (TagNode.class.isAssignableFrom(node.getClass())) {
TagNode tag = (TagNode) node;
if (tag.getName().equals(ElseIfTag.ELSEIF)) {
execute = isPositiveIfElseNode(tag, interpreter);
execute = !executedAnyBlock && isPositiveIfElseNode(tag, interpreter);
continue;
} else if (tag.getName().equals(ElseTag.ELSE)) {
execute = !executedAnyBlock;
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/IfTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public void itEvalsOnlyIfInElifTreeWhenExpIsTrue() {
assertThat(tag.interpret(n, interpreter).trim()).isEqualTo("ifblock");
}

@Test
public void itEvalsOnlyIfInElifTreeWhenBothAreTrue() {
context.put("foo", "bar");
TagNode n = fixture("if-true-elif-true");
assertThat(tag.interpret(n, interpreter).trim()).isEqualTo("one");
}

@Test
public void itEvalsOnlyElifInTreeWhenExp2IsTrue() {
context.put("foo", "");
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/tags/iftag/if-true-elif-true.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if true %}
one
{% elif true %}
two
{% endif %}