Skip to content

Commit

Permalink
Merge pull request #1159 from HubSpot/render-filter-no-extends-roots
Browse files Browse the repository at this point in the history
Use renderFlat inside of the RenderFilter
  • Loading branch information
jasmith-hs authored Feb 23, 2024
2 parents b535c9b + 5844e1b commit d576527
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ public Node parse(String template) {
* @return rendered result
*/
public String renderFlat(String template) {
return renderFlat(template, config.getMaxOutputSize());
}

/**
* Parse the given string into a root Node, and then render it without processing any extend parents.
* This method should be used when the template is known to not have any extends or block tags.
*
* @param template
* string to parse
* @param renderLimit
* stop rendering once this output length is reached
* @return rendered result
*/
public String renderFlat(String template, long renderLimit) {
int depth = context.getRenderDepth();

try {
Expand All @@ -246,7 +260,7 @@ public String renderFlat(String template) {
return template;
} else {
context.setRenderDepth(depth + 1);
return render(parse(template), false);
return render(parse(template), false, renderLimit);
}
} finally {
context.setRenderDepth(depth);
Expand All @@ -264,6 +278,15 @@ public String render(String template) {
return render(template, config.getMaxOutputSize());
}

/**
* Parse the given string into a root Node, and then renders it processing extend parents.
*
* @param template
* string to parse
* @param renderLimit
* stop rendering once this output length is reached
* @return rendered result
*/
public String render(String template, long renderLimit) {
return render(parse(template), true, renderLimit);
}
Expand Down Expand Up @@ -299,7 +322,7 @@ public String render(Node root, boolean processExtendRoots) {
* @param processExtendRoots
* if true, also render all extend parents
* @param renderLimit
* the number of characters the result may contain
* stop rendering once this output length is reached
* @return rendered result
*/
private String render(Node root, boolean processExtendRoots, long renderLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public String getName() {
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
if (args.length > 0) {
String firstArg = args[0];
return interpreter.render(
return interpreter.renderFlat(
Objects.toString(var),
NumberUtils.toLong(
firstArg,
JinjavaConfig.newBuilder().build().getMaxOutputSize()
)
);
}
return interpreter.render(Objects.toString(var));
return interpreter.renderFlat(Objects.toString(var));
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@ public void itDoesNotAlwaysCompleteHtmlTags() {
assertThat(filter.filter(stringToRender, interpreter, "17"))
.isEqualTo("<p> Hello, world!");
}

@Test
public void itDoesNotProcessExtendsRoots() {
String stringToRender =
"{% extends 'filter/render/base.jinja' -%}\n" +
"{% block body %}\n" +
"I am the extension body\n" +
"{% endblock %}" +
"You should never see this text in the output!\n" +
"{%- set foo = '{{ 1 + 1}}'|render -%}\n" +
"{% block footer %}\n" +
"I am the extension footer\n" +
"{% endblock %}";

assertThat(interpreter.render(stringToRender))
.isEqualTo(
"Body is: \n" +
"I am the extension body\n" +
"\n" +
"Footer is: \n" +
"I am the extension footer\n" +
"\n"
);
}
}
6 changes: 6 additions & 0 deletions src/test/resources/filter/render/base.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Body is: {% block body %}
Base body
{% endblock %}
Footer is: {% block footer %}
Base footer
{% endblock %}

0 comments on commit d576527

Please sign in to comment.