Comments (5)
btw - I really like the project. I have a fork of it where I'm working on a few things to help with the way I need to use it. It's a partial port to kotlin, so it's a bit of a mess, but there might be something that interests you.
- Making the engine more stateless (getting rid of statics)
- Creating components for separate concerns: parsing (TemplateFactory), and rendering (TemplateEngine)
- Making the engine immutable as much as possible, with "thaw" and builder support
- Added "accessor" logic that can support a variety of different template input models, including lazy-loading maps, reflection, fallback resolution, etc.
- Configurable caching for parsed templates, models, and accessors
- Allowing custom tags to produce per-node instances that do calculations up-front where possible
- Expanded nested RenderContext to a more formal stack/frame so tags can interact with other tags without polluting the variable space. This could also could support some level of variable shading, though I don't know if the liquid "spec" really defines much about that
- AssertJ assertions. They allow this type of syntax (this uses kotlin extension functions):
TemplateFactory.newInstance()
.parse("{% for i in (1..10000) %}{{ i }}{% endfor %}")
.rendering(model="{}", settings={
maxRenderTimeMillis = 1
executor = Executors.newSingleThreadExecutor()
}
.hasRenderError(TimeoutException::class.java)
In java, it would be like this:
Template parsed = TemplateFactory.newInstance()
.parse("{% for i in (1..10000) %}{{ i }}{% endfor %}");
assertThat(parsed)
.rendering("{}", settings->
settings.maxRenderTimeMillis(1)
settings.executor(Executors.newSingleThreadExecutor())
)
.hasRenderError(TimeoutException::class.java)
The fork is at https://github.com/kidgamesclub/Liqp/ - like I said, it's pretty messy right now.
from liqp.
Here's a simple test to verify the template above, I'm happy to submit it as a PR:
package liqp.filters;
import static org.hamcrest.core.IsEqual.equalTo;
import liqp.Template;
import liqp.TemplateContext;
import liqp.nodes.LNode;
import liqp.tags.Tag;
import org.antlr.runtime.RecognitionException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class CustomTagNesting {
@BeforeClass
public static void registerTags() {
Tag.registerTag(new XmlTag("red"));
Tag.registerTag(new XmlTag("bold"));
Tag.registerTag(new XmlTag("yellow"));
Tag.registerTag(new XmlTag("green"));
}
@Test
public void applyTest() throws RecognitionException {
final String rendered = Template.parse("{%red%}{%bold%}Red and {%yellow%}yellow{%endyellow%} and " +
"{%green%}green{%endgreen%}{%endbold%}{%red%}")
.render("{}");
Assert.assertThat(rendered, equalTo("<red><bold>Red and <yellow>yellow</yellow> and " +
"<green>green</green></bold></red>"));
}
public static class XmlTag extends Tag {
public XmlTag(String tag) {
super(tag);
}
@Override
public Object render(TemplateContext context, LNode... nodes) {
final StringBuilder output = new StringBuilder();
output.append("<").append(this.name).append(">");
for (LNode node : nodes) {
output.append(node.render(context));
}
output.append("</").append(this.name).append(">");
return output.toString();
}
}
}
The test fails with this output:
java.lang.AssertionError:
Expected: "<red><bold>Red and <yellow>yellow</yellow> and <green>green</green></bold></red>"
got: "<red><bold>Red and <yellow>yellow</yellow> and <green></green>green</bold></red><red></red>"
Expected :"<red><bold>Red and <yellow>yellow</yellow> and <green>green</green></bold></red>"
Actual :"<red><bold>Red and <yellow>yellow</yellow> and <green></green>green</bold></red><red></red>"
from liqp.
Yes. I'm an idiot. My template was wrong.
from liqp.
😄
Good to see you have it sorted!
from liqp.
Nice, Eric! Thanks!
from liqp.
Related Issues (20)
- Upgrade ANTLR HOT 11
- Template errors not being stored when using liquid for loops HOT 2
- Support for Liquid's Layouts HOT 3
- Integer and Float Comparisons After Using Times Filter HOT 15
- How to modify default renderer HOT 1
- Shaded jar should exclude non-ANTLR dependencies HOT 2
- Support for `liquid` tag HOT 1
- expression errors ignored in _some_ cases HOT 1
- Template parse is deprecated HOT 3
- Issues with the new release. HOT 2
- != working as == HOT 2
- Is Template (render) thread safe? HOT 2
- when text contains UNICODE characters the parsing fails HOT 1
- Parser error "Invalid Tag: 'liquid'" HOT 2
- Support Instant datetime pattern
- Support for Instant, LocalDateTime date pattern HOT 1
- BlockNode Map render issue: String.valueOf(Map<?,?>) returns '=' instead of ':' as delimiter HOT 12
- Strict typing enforcement cannot be fully disabled HOT 3
- Applying strip_html filter to escaped html will unescape the string HOT 6
- Variable Name with Hyphen Misinterpreted in Conditional Statements HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liqp.