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

The render settings from the main template is not propagated to the I… #140

Merged
merged 1 commit into from
Apr 26, 2019
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
1 change: 1 addition & 0 deletions snippets/wmt/footer-with-variables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ FOOTERTEXT }}
45 changes: 45 additions & 0 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ private Template(String input, Map<String, Tag> tags, Map<String, Filter> filter
}

/**
* Creates a new Template instance from a given input.
* @param input
* the file holding the Liquid source.
* @param tags
* the tags this instance will make use of.
* @param filters
* the filters this instance will make use of.
* @param parseSettings
* the parseSettings this instance will make use of.
* @param renderSettings
* the renderSettings this instance will make use of.
*/
private Template(String input, Map<String, Tag> tags, Map<String, Filter> filters, ParseSettings parseSettings,
RenderSettings renderSettings) {

this(input, tags, filters, parseSettings);
this.renderSettings = renderSettings;
}

/**
* Creates a new Template instance from a given file.
*
* @param file
Expand All @@ -102,6 +122,27 @@ private Template(File file, Map<String, Tag> tags, Map<String, Filter> filters,
}
}

/**
* Creates a new Template instance from a given file.
*
* @param file
* the file holding the Liquid source.
* @param tags
* the tags this instance will make use of.
* @param filters
* the filters this instance will make use of.
* @param parseSettings
* the parseSettings this instance will make use of.
* @param renderSettings
* the renderSettings this instance will make use of.
*/
private Template(File file, Map<String, Tag> tags, Map<String, Filter> filters, ParseSettings parseSettings,
RenderSettings renderSettings) throws IOException {

this(file, tags, filters, parseSettings);
this.renderSettings = renderSettings;
}

private ParseTree parse(LiquidLexer lexer) {

lexer.removeErrorListeners();
Expand Down Expand Up @@ -177,6 +218,10 @@ public static Template parse(String input, ParseSettings settings) {
return new Template(input, Tag.getTags(), Filter.getFilters(), settings);
}

public static Template parse(File file, ParseSettings parseSettings, RenderSettings renderSettings) throws IOException {
return new Template(file, Tag.getTags(), Filter.getFilters(), parseSettings, renderSettings);
}

@Deprecated // Use `parse(file, settings)` instead
public static Template parse(File file, Flavor flavor) throws IOException {
ParseSettings settings = new ParseSettings.Builder().withFlavor(flavor).build();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/liqp/tags/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public Object render(TemplateContext context, LNode... nodes) {
else {
includeResourceFile = new File(context.parseSettings.flavor.snippetsFolderName, includeResource + extension);
}
Template template = Template.parse(includeResourceFile, context.parseSettings);

Template template = Template.parse(includeResourceFile, context.parseSettings, context.renderSettings);

// check if there's a optional "with expression"
if(nodes.length > 1) {
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package liqp.tags;

import liqp.*;
import liqp.exceptions.VariableNotExistException;
import liqp.filters.Filter;
import liqp.parser.Flavor;
import org.antlr.v4.runtime.RecognitionException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.matchers.ThrowableCauseMatcher;
import org.junit.rules.ExpectedException;
import org.hamcrest.Matcher;

import java.io.File;
import java.io.IOException;
Expand All @@ -15,10 +20,14 @@
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.*;

public class IncludeTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void init() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = Filter.class.getDeclaredMethod("resetFilters");
Expand Down Expand Up @@ -106,6 +115,35 @@ public void renderTestWithIncludeSubdirectorySpecifiedInJekyllFlavor() throws Ex
assertTrue(result.contains("FOOTER"));
}

@Test
public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavorWithStrictVariables() throws Exception {

String expected = "Sample Footer";
File index = new File("src/test/jekyll/index_with_variables.html");
Template template = Template.parse(
index,
new ParseSettings.Builder().withFlavor(Flavor.LIQUID).build(),
new RenderSettings.Builder().withStrictVariables(true).withShowExceptionsFromInclude(true).build());
Map<String, Object> variables = new HashMap<>();
variables.put("FOOTERTEXT", expected);
String result = template.render(variables);
assertTrue(result.contains(expected));
}


@Test
public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavorWithStrictVariablesException() throws Exception {

thrown.expectCause(new NestedCauseMatcher<>(isA(VariableNotExistException.class)));

File index = new File("src/test/jekyll/index_with_variables.html");
Template template = Template.parse(
index,
new ParseSettings.Builder().withFlavor(Flavor.LIQUID).build(),
new RenderSettings.Builder().withStrictVariables(true).withShowExceptionsFromInclude(true).build());
template.render();
}

// https://github.com/bkiers/Liqp/issues/95
@Test
public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavor() throws Exception {
Expand Down Expand Up @@ -211,4 +249,31 @@ public void errorInIncludeCauseMissingIncludeWithCustomRenderingAndFixedError()
// them
assertTrue(result.contains("THE_ERROR"));
}

private static class NestedCauseMatcher<T extends Throwable> extends ThrowableCauseMatcher<T> {

private Matcher<? extends Throwable> causeMatcher;

public NestedCauseMatcher(Matcher<T> causeMatcher) {

super(causeMatcher);
this.causeMatcher = causeMatcher;
}

@Override
protected boolean matchesSafely(T item) {

boolean match = false;
Throwable parent = item;
Throwable cause = null;

while (((cause = parent.getCause()) != null) && (cause != parent) && !(match = causeMatcher.matches(cause))) {

parent = cause;
}

return match;
}

}
}
1 change: 1 addition & 0 deletions src/test/jekyll/index_with_variables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include 'wmt/footer-with-variables.html' %}