From e2dbd77ac7474e7e054c853823fe4dda13d257fe Mon Sep 17 00:00:00 2001 From: Vasyl Khrystiuk Date: Fri, 29 Mar 2019 18:06:04 +0200 Subject: [PATCH] close https://github.com/bkiers/Liqp/issues/129 --- src/main/java/liqp/TemplateContext.java | 18 ++++++++------- src/main/java/liqp/tags/Include.java | 10 ++++----- src/test/java/liqp/tags/IncludeTest.java | 22 ++++++++++++++++++- .../jekyll/alternative_includes/header.html | 1 + 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/test/jekyll/alternative_includes/header.html diff --git a/src/main/java/liqp/TemplateContext.java b/src/main/java/liqp/TemplateContext.java index c75d4f7a..80c7830e 100644 --- a/src/main/java/liqp/TemplateContext.java +++ b/src/main/java/liqp/TemplateContext.java @@ -10,31 +10,33 @@ public class TemplateContext { protected TemplateContext parent; public final ProtectionSettings protectionSettings; public final RenderSettings renderSettings; - public final Flavor flavor; + public final ParseSettings parseSettings; private Map variables; public TemplateContext() { this(new ProtectionSettings.Builder().build(), new RenderSettings.Builder().build(), - Flavor.LIQUID, + new ParseSettings.Builder().withFlavor(Flavor.LIQUID).build(), new LinkedHashMap()); } - public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, Flavor flavor, + @Deprecated // Use `TemplateContext(protectionSettings, renderSettings, parseSettings, variables)` instead + public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, Flavor flavor, Map variables) { + this(protectionSettings, renderSettings, new ParseSettings.Builder().withFlavor(flavor).build(), variables); + } + + public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, ParseSettings parseSettings, Map variables) { this.parent = null; this.protectionSettings = protectionSettings; this.renderSettings = renderSettings; - this.flavor = flavor; + this.parseSettings = parseSettings; this.variables = new LinkedHashMap(variables); } public TemplateContext(TemplateContext parent) { + this(parent.protectionSettings, parent.renderSettings, parent.parseSettings, new LinkedHashMap()); this.parent = parent; - this.protectionSettings = parent.protectionSettings; - this.renderSettings = parent.renderSettings; - this.flavor = parent.flavor; - this.variables = new LinkedHashMap(); } public void incrementIterations() { diff --git a/src/main/java/liqp/tags/Include.java b/src/main/java/liqp/tags/Include.java index 636122d8..732817ef 100644 --- a/src/main/java/liqp/tags/Include.java +++ b/src/main/java/liqp/tags/Include.java @@ -20,15 +20,15 @@ public Object render(TemplateContext context, LNode... nodes) { if(includeResource.indexOf('.') > 0) { extension = ""; } - File includeResourceFile; + File includeResourceFile; File includesDirectory = (File) context.get(INCLUDES_DIRECTORY_KEY); if (includesDirectory != null) { includeResourceFile = new File(includesDirectory, includeResource + extension); - } + } else { - includeResourceFile = new File(context.flavor.snippetsFolderName, includeResource + extension); - } - Template template = Template.parse(includeResourceFile, context.flavor); + includeResourceFile = new File(context.parseSettings.flavor.snippetsFolderName, includeResource + extension); + } + Template template = Template.parse(includeResourceFile, context.parseSettings); // check if there's a optional "with expression" if(nodes.length > 1) { Object value = nodes[1].render(context); diff --git a/src/test/java/liqp/tags/IncludeTest.java b/src/test/java/liqp/tags/IncludeTest.java index 117852e7..e4cbd275 100644 --- a/src/test/java/liqp/tags/IncludeTest.java +++ b/src/test/java/liqp/tags/IncludeTest.java @@ -6,6 +6,9 @@ import org.junit.Test; import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; @@ -38,7 +41,7 @@ public void renderTest() throws RecognitionException { "color: 'red'\n" + "shape: 'square'")); } - + @Test public void renderTestWithIncludeDirectorySpecifiedInContextLiquidFlavor() throws Exception { File jekyll = new File(new File("").getAbsolutePath(), "src/test/jekyll"); @@ -131,4 +134,21 @@ public void expressionInIncludeTagDefaultFlavorThrowsException() { Template.parse(source).render(); } + + @Test + public void includeDirectoryKeyInInputShouldChangeIncludeDirectory() throws IOException { + // given + File jekyll = new File(new File("").getAbsolutePath(), "src/test/jekyll"); + File index = new File(jekyll, "index_without_quotes.html"); + Template template = Template.parse(index, new ParseSettings.Builder().withFlavor(Flavor.JEKYLL).build()); + Map data = new HashMap(); + data.put(Include.INCLUDES_DIRECTORY_KEY, new File(new File("").getAbsolutePath(), "src/test/jekyll/alternative_includes")); + + // when + + String result = template.render(data); + + // then + assertTrue(result.contains("ALTERNATIVE")); + } } diff --git a/src/test/jekyll/alternative_includes/header.html b/src/test/jekyll/alternative_includes/header.html new file mode 100644 index 00000000..8892711b --- /dev/null +++ b/src/test/jekyll/alternative_includes/header.html @@ -0,0 +1 @@ +ALTERNATIVE