Skip to content

Commit

Permalink
new renderer helper 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasyl Khrystiuk committed Mar 30, 2019
1 parent 423c8f4 commit 2eb6bc8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
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
8 changes: 4 additions & 4 deletions src/main/java/liqp/tags/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public Object render(TemplateContext context, LNode... nodes) {
if(includeResource.indexOf('.') > 0) {
extension = "";
}
File includeResourceFile;
File includesDirectory = (File) context.get(INCLUDES_DIRECTORY_KEY);
File includeResourceFile;
String includesDirectory = (String) 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);
// check if there's a optional "with expression"
if(nodes.length > 1) {
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"));
}

}
40 changes: 39 additions & 1 deletion src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -131,4 +134,39 @@ 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<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_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, "alternative_includes");

// when

String result = template.render(data);

// then
assertTrue(result.contains("ALTERNATIVE"));
}
}
1 change: 1 addition & 0 deletions src/test/jekyll/alternative_includes/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTERNATIVE

0 comments on commit 2eb6bc8

Please sign in to comment.