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

Ck/template context #279

Merged
merged 2 commits into from
Jul 30, 2023
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
4 changes: 2 additions & 2 deletions src/main/java/liqp/RenderTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* created temporarily on the heap, increasing the chance of an out-of-memory situation, and a generally
* high allocation activity (spikes in the memory allocation graph).
* <p>
* For example, the following liquid template results in <em>10012</em> {@link StringBuilder}
* {@link #toString()} conversions:
* For example, the following liquid template results in <em>10012</em>
* {@link StringBuilder#toString()} conversions:
*
* <pre><code>{% for l in (1..10)%}{% for k in (1..1000) %}
* {% for i in (1..1000) %}Hello! {{ i }} world
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import liqp.RenderTransformer.ObjectAppender;
import liqp.exceptions.ExceededMaxIterationsException;
import liqp.parser.LiquidSupport;
import liqp.filters.Filters;
import liqp.parser.Flavor;

public class TemplateContext {

Expand All @@ -29,6 +31,7 @@ public class TemplateContext {
private Map<String, Object> registry;

private List<Exception> errors;
private final Template template;

public TemplateContext() {
this(TemplateParser.DEFAULT, new LinkedHashMap<>());
Expand All @@ -40,14 +43,19 @@ public TemplateContext(Map<String, Object> variables) {
}

public TemplateContext(TemplateParser parser, Map<String, Object> variables) {
this(null, parser, variables);
}

public TemplateContext(Template template, TemplateParser parser, Map<String, Object> variables) {
this.template = template;
this.parent = null;
this.parser = parser;
this.variables = new LinkedHashMap<>(variables);
this.errors = new ArrayList<>();
}

public TemplateContext(TemplateContext parent) {
this(parent.getParser(), new LinkedHashMap<String, Object>());
this(parent.template, parent.getParser(), new LinkedHashMap<String, Object>());
this.parent = parent;
}

Expand Down Expand Up @@ -199,4 +207,9 @@ public void checkForMaxIterations(int iterations) {
public TemplateParser.ErrorMode getErrorMode() {
return parser.getErrorMode();
}

public TemplateContext newChildContext() {
return newChildContext(new HashMap<>());
}

}
2 changes: 1 addition & 1 deletion src/main/java/liqp/blocks/For.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Object render(TemplateContext context, LNode... nodes) {
boolean reversed = super.asBoolean(nodes[6].render(context));

// Each for tag has its own context that keeps track of its own variables (scope)
TemplateContext nestedContext = new TemplateContext(context);
TemplateContext nestedContext = context.newChildContext();

Object rendered = array ? renderArray(id, nestedContext, tagName, reversed, nodes) : renderRange(id, nestedContext, tagName, reversed, nodes);

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/liqp/blocks/Tablerow.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package liqp.blocks;

import liqp.TemplateContext;
import liqp.RenderTransformer.ObjectAppender;
import liqp.nodes.LNode;
import liqp.parser.LiquidSupport;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import liqp.RenderTransformer.ObjectAppender;
import liqp.TemplateContext;
import liqp.nodes.LNode;
import liqp.parser.LiquidSupport;

public class Tablerow extends Block {

private static final String COLS = "cols";
Expand Down Expand Up @@ -66,7 +66,7 @@ public Object render(TemplateContext context, LNode... nodes) {
}
}

TemplateContext nestedContext = new TemplateContext(context);
TemplateContext nestedContext = context.newChildContext();
int total = Math.min(collection.length, limit);
TablerowloopDrop tablerowloopDrop = new TablerowloopDrop(total, cols);
nestedContext.put(TABLEROWLOOP, tablerowloopDrop);
Expand Down
Loading