Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use renderFlat inside of the RenderFilter #1159

Merged
merged 2 commits into from
Feb 23, 2024
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 @@ -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 %}
Loading