Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ protected AstNode nonliteral() throws ScanException, ParseException {
switch (getToken().getSymbol()) {
case IDENTIFIER:
String name = consumeToken().getImage();
if (getToken().getSymbol() == COLON) {
if (getToken().getSymbol() == COLON && getToken().getImage().equals(":")) {
Symbol lookahead = lookahead(0).getSymbol();
if (
isPossibleExpTest(lookahead) &&
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/MacroTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ public void itCorrectlyScopesNestedMacroTags() {
}
}

@Test
public void itCallsMacroInTernaryWithVariableCondition() {
String template =
"{% macro greet(name) %}Hello {{ name }}{% endmacro %}" +
"{{ greet('world') if myVar else greet('nobody') }}";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an adjacent test for a ternary using ? and : too to ensure that's also parsing properly

{{ myVar ? greet('world') : greet('nobody') }}


context.put("myVar", true);
assertThat(jinjava.render(template, context).trim()).isEqualTo("Hello world");

context.put("myVar", false);
assertThat(jinjava.render(template, context).trim()).isEqualTo("Hello nobody");
}

@Test
public void itCallsMacroInStandardTernaryWithVariableCondition() {
String template =
"{% macro greet(name) %}Hello {{ name }}{% endmacro %}" +
"{{ myVar ? greet('world') : greet('nobody') }}";

context.put("myVar", true);
assertThat(jinjava.render(template, context).trim()).isEqualTo("Hello world");

context.put("myVar", false);
assertThat(jinjava.render(template, context).trim()).isEqualTo("Hello nobody");
}

private Node snippet(String jinja) {
return new TreeParser(interpreter, jinja).buildTree().getChildren().getFirst();
}
Expand Down
Loading