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

new renderer helper 3 #136

Merged
merged 2 commits into from
Apr 23, 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 alternative_includes/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTERNATIVE
16 changes: 14 additions & 2 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package liqp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import liqp.filters.Filter;
import liqp.nodes.LNode;
import liqp.parser.Flavor;
import liqp.parser.v4.NodeVisitor;
import liqp.tags.Include;
import liqp.tags.Tag;
import liquid.parser.v4.LiquidLexer;
import liquid.parser.v4.LiquidParser;
Expand Down Expand Up @@ -122,7 +124,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
throw new RuntimeException(String.format("parser error on line %s, index %s", line, charPositionInLine), e);
}
});

parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
try {
return parser.parse();
Expand Down Expand Up @@ -335,10 +337,20 @@ public String call() {
* @return a string denoting the rendered template.
*/
public String renderUnguarded(final Map<String, Object> variables) {

if (variables.containsKey(Include.INCLUDES_DIRECTORY_KEY)) {
Object includeDirectory = variables.get(Include.INCLUDES_DIRECTORY_KEY);
if (includeDirectory instanceof File) {
variables.put(Include.INCLUDES_DIRECTORY_KEY, ((File) includeDirectory).getAbsolutePath());
}
}
ObjectNode value = parseSettings.mapper.convertValue(variables, ObjectNode.class);
Map map = parseSettings.mapper.convertValue(value, Map.class);

final NodeVisitor visitor = new NodeVisitor(this.tags, this.filters, this.parseSettings);
try {
LNode node = visitor.visit(root);
Object rendered = node.render(new TemplateContext(protectionSettings, renderSettings, parseSettings.flavor, variables));
Object rendered = node.render(new TemplateContext(protectionSettings, renderSettings, parseSettings.flavor, map));
return rendered == null ? "" : String.valueOf(rendered);
}
catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/liqp/tags/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public Object render(TemplateContext context, LNode... nodes) {
}
File includeResourceFile;
File includesDirectory = (File) context.get(INCLUDES_DIRECTORY_KEY);

if (includesDirectory != null) {
includeResourceFile = new File(includesDirectory, includeResource + extension);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/liqp/TemplateTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package liqp;

import org.antlr.v4.runtime.RecognitionException;
import org.junit.Ignore;
import org.junit.Test;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class TemplateTest {
Expand Down Expand Up @@ -80,4 +85,17 @@ public void renderVarArgsTest() throws RecognitionException {
public void renderVarArgsTestInvalidKey2() throws RecognitionException {
Template.parse("mu").render(null, 456);
}

@Test
public void renderMapWithPojosExistedNotRender() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("foo", new Foo());
data.put("bar", "zoo");
data.put("bear", true);

String fooA = Template.parse("{{foo.a}}{{bar}}{{bear}}").render(data);

assertThat(fooA, is("Azootrue"));
}

}
21 changes: 19 additions & 2 deletions src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,24 @@ public void expressionInIncludeTagDefaultFlavorThrowsException() {
}

@Test
public void errorInIncludeCauseMissingIncludeWithDefaultRendering() throws IOException {
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<String, Object> data = new HashMap<String, Object>();
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"));
}

@Test
public void includeDirectoryKeyStringInInputShouldChangeIncludeDirectory() throws IOException {
//given
File jekyll = new File(new File("").getAbsolutePath(), "src/test/jekyll");
File index = new File(jekyll, "index_with_errored_include.html");
Expand Down Expand Up @@ -203,7 +220,7 @@ public void includeDirectoryKeyInInputShouldChangeIncludeDirectory() throws IOEx
File index = new File(jekyll, "index_without_quotes.html");
Template template = Template.parse(index, new ParseSettings.Builder().withFlavor(Flavor.JEKYLL).build());
Map<String, Object> data = new HashMap<String, Object>();
data.put(Include.INCLUDES_DIRECTORY_KEY, new File(new File("").getAbsolutePath(), "src/test/jekyll/alternative_includes"));
data.put(Include.INCLUDES_DIRECTORY_KEY, "alternative_includes");

// when

Expand Down