Skip to content

Commit efc07af

Browse files
author
Achim Abeling
committed
IncludeTag now respects ignore missing
1 parent 8eaf40e commit efc07af

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/main/java/com/hubspot/jinjava/lib/tag/IncludeTag.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
7373
);
7474
templateFile = interpreter.resolveResourceLocation(templateFile);
7575

76+
/* check next tokens if it could be ignored if missing */
77+
boolean ignoreMissing = checkIgnoreMissing(helper);
78+
7679
try {
7780
interpreter
7881
.getContext()
@@ -108,6 +111,9 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
108111

109112
return interpreter.render(node, false);
110113
} catch (IOException e) {
114+
115+
if (ignoreMissing) return "";
116+
111117
throw new InterpretException(
112118
e.getMessage(),
113119
e,
@@ -120,6 +126,20 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
120126
}
121127
}
122128

129+
/**
130+
* Returns true if the next two tokens are "ignore" and "missing"
131+
* @param helper
132+
*/
133+
private boolean checkIgnoreMissing(HelperStringTokenizer helper) {
134+
135+
if (helper.hasNext() && "ignore".equals(helper.next())) {
136+
if (helper.hasNext() && "missing".equals(helper.next())) {
137+
return true;
138+
}
139+
}
140+
return false;
141+
}
142+
123143
@Override
124144
public String getEndTagName() {
125145
return null;

src/test/java/com/hubspot/jinjava/lib/tag/IncludeTagTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,16 @@ public void itAvoidsTagCycleExceptionInsideExtendedFiles() throws Exception {
207207
);
208208
assertThat(result).isEqualTo("Extended text, will be rendered");
209209
}
210+
211+
@Test
212+
public void itIgnoresMissing() throws IOException {
213+
String result = jinjava.render(
214+
Resources.toString(
215+
Resources.getResource("tags/includetag/missing-include.jinja"),
216+
StandardCharsets.UTF_8
217+
),
218+
new HashMap<String, Object>()
219+
);
220+
assertThat(result).containsSequence("AB");
221+
}
210222
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A{% include "foobar" ignore missing %}B

0 commit comments

Comments
 (0)