Skip to content

Commit

Permalink
TemplateContext: provide access to Filters, Insertions, parent context
Browse files Browse the repository at this point in the history
Expose Filters and Insertions from a TemplateContext, and simplify
creating a new child context by adding a newChildContext() helper
method.
  • Loading branch information
kohlschuetter committed Jun 27, 2023
1 parent 5a1a806 commit 4a4be2f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 55 deletions.
59 changes: 12 additions & 47 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package liqp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -18,7 +17,6 @@

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
Expand Down Expand Up @@ -69,21 +67,7 @@ public class Template {

private ContextHolder contextHolder;

private TemplateParser templateParser = null;

/**
* Creates a new Template instance from a given input.
*
* @param input
* the file holding the Liquid source.
* @param insertions
* the insertions this instance will make use of.
* @param filters
* the filters this instance will make use of.
*/
private Template(String input, Insertions insertions, Filters filters, ParseSettings parseSettings) {
this(CharStreams.fromString(input, input), insertions, filters, parseSettings);
}
private TemplateParser templateParser;

private Template(CharStream stream, Insertions insertions, Filters filters, ParseSettings parseSettingsIn) {
this.templateParser = new TemplateParser.Builder()
Expand Down Expand Up @@ -111,37 +95,10 @@ private Template(CharStream stream, Insertions insertions, Filters filters, Pars
}
}

/**
* Creates a new Template instance from a given file.
*
* @param file
* the file holding the Liquid source.
*/
private Template(File file, Insertions insertions, Filters filters, ParseSettings parseSettings)
throws IOException {
this(fromFile(file), insertions, filters, parseSettings);
}

// TemplateParser constructor
Template(TemplateParser parser, CharStream input) {
this(input, parser.getParseSettings().insertions, parser.getParseSettings().filters, parser.getParseSettings());
this.templateParser = parser;
}

private static CharStream fromStream(InputStream in) {
try {
return CharStreams.fromStream(in);
} catch (IOException e) {
throw new RuntimeException("could not parse input: " + in, e);
}
}

private static CharStream fromFile(File path) {
try {
return CharStreams.fromFileName(path.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException("could not parse input: " + path, e);
}
this.templateParser = Objects.requireNonNull(parser);
}

private ParseTree parse(LiquidLexer lexer) {
Expand Down Expand Up @@ -410,7 +367,7 @@ public String renderUnguarded(Map<String, Object> variables, TemplateContext par
}

private TemplateContext newRootContext(Map<String, Object> variables) {
TemplateContext context = new TemplateContext(templateParser, variables);
TemplateContext context = new TemplateContext(this, templateParser, variables);
Consumer<Map<String, Object>> configurator = context.getRenderSettings().getEnvironmentMapConfigurator();
if (configurator != null) {
configurator.accept(context.getEnvironmentMap());
Expand Down Expand Up @@ -576,4 +533,12 @@ private void putStringKey(boolean convertValueToMap, String key, Object value,
TemplateParser getTemplateParser() {
return templateParser;
}

Filters getFilters() {
return filters;
}

Insertions getInsertions() {
return insertions;
}
}
30 changes: 29 additions & 1 deletion src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import liqp.RenderTransformer.ObjectAppender;
import liqp.parser.LiquidSupport;
import liqp.filters.Filters;
import liqp.parser.Flavor;

public class TemplateContext {

Expand All @@ -28,6 +30,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 @@ -41,14 +44,19 @@ public TemplateContext(ProtectionSettings protectionSettings, RenderSettings ren
}

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 @@ -220,4 +228,24 @@ public TemplateContext newChildContext(Map<String, Object> variablesForChild) {
public TemplateParser.ErrorMode getErrorMode() {
return parser.getErrorMode();
}

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

public Filters getFilters() {
if(template == null) {
return getParseSettings().filters;
} else {
return template.getFilters();
}
}

public Insertions getInsertions() {
if(template == null) {
return getParseSettings().insertions;
} else {
return template.getInsertions();
}
}
}
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

0 comments on commit 4a4be2f

Please sign in to comment.