Skip to content

Commit a243eab

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

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,10 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
108111

109112
return interpreter.render(node, false);
110113
} catch (IOException e) {
114+
if (ignoreMissing) {
115+
return "";
116+
}
117+
111118
throw new InterpretException(
112119
e.getMessage(),
113120
e,
@@ -120,6 +127,19 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
120127
}
121128
}
122129

130+
/**
131+
* Returns true if the next two tokens are "ignore" and "missing"
132+
* @param helper
133+
*/
134+
private boolean checkIgnoreMissing(HelperStringTokenizer helper) {
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)