Skip to content

Commit

Permalink
IncludeTag now respects ignore missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Achim Abeling committed Feb 25, 2022
1 parent 8eaf40e commit a243eab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/tag/IncludeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
);
templateFile = interpreter.resolveResourceLocation(templateFile);

/* check next tokens if it could be ignored if missing */
boolean ignoreMissing = checkIgnoreMissing(helper);

try {
interpreter
.getContext()
Expand Down Expand Up @@ -108,6 +111,10 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {

return interpreter.render(node, false);
} catch (IOException e) {
if (ignoreMissing) {
return "";
}

throw new InterpretException(
e.getMessage(),
e,
Expand All @@ -120,6 +127,19 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
}
}

/**
* Returns true if the next two tokens are "ignore" and "missing"
* @param helper
*/
private boolean checkIgnoreMissing(HelperStringTokenizer helper) {
if (helper.hasNext() && "ignore".equals(helper.next())) {
if (helper.hasNext() && "missing".equals(helper.next())) {
return true;
}
}
return false;
}

@Override
public String getEndTagName() {
return null;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/IncludeTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,16 @@ public void itAvoidsTagCycleExceptionInsideExtendedFiles() throws Exception {
);
assertThat(result).isEqualTo("Extended text, will be rendered");
}

@Test
public void itIgnoresMissing() throws IOException {
String result = jinjava.render(
Resources.toString(
Resources.getResource("tags/includetag/missing-include.jinja"),
StandardCharsets.UTF_8
),
new HashMap<String, Object>()
);
assertThat(result).containsSequence("AB");
}
}
1 change: 1 addition & 0 deletions src/test/resources/tags/includetag/missing-include.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A{% include "foobar" ignore missing %}B

0 comments on commit a243eab

Please sign in to comment.