A Java implementation of the Liquid templating engine backed up by an ANTLR grammar.
Add the dependency:
dependencies {
compile 'nl.big-o:liqp:0.9'
}
Add the following dependency:
<dependency>
<groupId>nl.big-o</groupId>
<artifactId>liqp</artifactId>
<version>0.9</version>
</dependency>
Or clone this repository and run: mvn install
which will create a JAR of Liqp
in your local Maven repository, as well as in the project's target/
folder.
This library can be used in two different ways:
- to construct a parse tree of some Liquid input
- to render Liquid input source (either files, or input strings)
To create a parse tree from input source, do the following:
String input =
"<ul id=\"products\"> \n" +
" {% for product in products %} \n" +
" <li> \n" +
" <h2>{{ product.name }}</h2> \n" +
" Only {{ product.price | price }} \n" +
" \n" +
" {{ product.description | prettyprint | paragraph }} \n" +
" </li> \n" +
" {% endfor %} \n" +
"</ul> \n";
Template template = TemplateParser.DEFAULT.parse(input);
ParseTree root = template.getParseTree();
As you can see, the getParseTree()
method returns an instance of a
ParseTree
denoting the root
node of the input source. To see how the parse tree is built, you can use Template#toStringAST()
to print
an ASCII representation of the tree.
If you're not familiar with Liquid, have a look at their website: http://liquidmarkup.org.
In Ruby, you'd render a template like this:
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render( 'name' => 'tobi' ) # Renders the output => "hi tobi"
With Liqp, the equivalent looks like this:
TemplateParser parser = new TemplateParser.Builder().build();
Template template = parser.parse("hi {{name}}");
String rendered = template.render("name", "tobi");
System.out.println(rendered);
/*
hi tobi
*/
The template variables provided as parameters to render(...)
can be:
- a varargs where
the 0th, 2nd, 4th, ... indexes must be
String
literals denoting the keys. The values can be anyObject
. - a
Map<String, Object>
- a JSON string
- any POJO that is marked with special marker interface
liqp.parser.Inspectable
. In this case the object is converted tojava.util.Map
using jackson's mapper, and so all recipes for configuring jackson conversation will work here. - any object that extend special interface
liqp.parser.LiquidSupport
and it is designed for lazy field values computing. It's methodLiquidSupport#toLiquid()
is called only if/when the object is going to be rendered. SinceLiquidSupport
extendsInspectable
simply use same variant of therender(...)
method.
The following examples are equivalent to the previous Liqp example:
Template template = new TemplateParser.Builder().build().parse("hi {{name}}");
Map<String, Object> map = new HashMap<>();
map.put("name", "tobi");
String rendered = template.render(map);
System.out.println(rendered);
/*
hi tobi
*/
Template template = new TemplateParser.Builder().build().parse("hi {{name}}");
String rendered = template.render("{\"name\" : \"tobi\"}");
System.out.println(rendered);
/*
hi tobi
*/
class MyParams implements Inspectable {
public String name = "tobi";
};
Template template = TemplateParser.DEFAULT.parse("hi {{name}}");
String rendered = template.render(new MyParams());
System.out.println(rendered);
/*
hi tobi
*/
class MyLazy implements LiquidSupport {
@Override
public Map<String, Object> toLiquid() {
return Collections.singletonMap("name", "tobi");
}
};
Template template = TemplateParser.DEFAULT.parse("hi {{name}}");
String rendered = template.render(new MyLazy());
System.out.println(rendered);
/*
hi tobi
*/
The library has a set of keys to control the parsing/rendering process. Even if you might think that's too many of them, the defaults will work for you in most cases. All of them are set on TemplateParser.Builder
class. Here they are:
withFlavor(Flavor flavor)
- flavor of the liquid language. Flavor is nothing else than a predefined set of other settings. Here are supported flavors:Flavor.JEKYLL
- flavor that defines all settings, so it tries to behave like jekyll's templatesFlavor.LIQUID
- the same for liquid's templatesFlavor.LIQP
(default) - developer of this library found some default behavior of two flavors above somehow weird in selected cases. So this flavor appears.
withStripSingleLine(boolean stripSingleLine)
- iftrue
then all blank lines left by outputless tags are removed. Default isfalse
.withStripSpaceAroundTags(boolean stripSpacesAroundTags)
- iftrue
then all whitespaces around tags are removed. Default isfalse
.withObjectMapper(ObjectMapper mapper)
- if provided then this mapper is used for converting json strings to objects and internal object conversion. If not provided, then default mapper is used. Default one is good. Also, the default one is always accessible via TemplateContext instance:context.getParser().getMapper();
withTag(Tag tag)
- register custom tag to be used in templates.withBlock(Block block)
- register custom block to be used in templates. The difference between tag and block is that block has open and closing tag and can contain other content like a text, tags and blocks.withFilter(Filter filter)
- register custom filter to be used in templates. See below for examples.withEvaluateInOutputTag(boolean evaluateInOutputTag)
- bothFlavor.JEKYLL
andFlavor.LIQUID
are not allows to evaluate expressions in output tags, simply ignoring the expression and printing out very first token of the expression. Yes, this:{{ 97 > 96 }}
will print97
. This is known bug/feature of those templators. If you want to change this behavior and evaluate those expressions, set this flag totrue
. Also, the default flavorFlavor.LIQP
has this flag set totrue
already.withStrictTypedExpressions(boolean strictTypedExpressions)
- ruby is strong-typed language. So comparing different types is not allowed there. This library tries to mimic ruby's type system in a way so all not explicit types (created or manipulated inside of templates) are converted with this schema:nil
->null
;boolean
->boolean
;string
->java.lang.String
; any numbers ->java.math.BigDecimal
, any datetime ->java.time.ZonedDateTime
. If you want to change this behavior, and allow comparing in expressions in a less restricted way, set this flag totrue
, then the lax (javascript-like) approach for comparing in expressions will be used. Also, the default flavorFlavor.LIQP
has this flag set totrue
already, others have itfalse
by default.withLiquidStyleInclude(boolean liquidStyleInclude)
- iftrue
then include tag will use syntax from liquid, otherwice jekyll syntax will be used. Default depends on flavor.Flavor.LIQUID
andFlavor.LIQP
has this flag set totrue
already.Flavor.JEKYLL
has itfalse
.withStrictVariables(boolean strictVariables)
- if set totrue
then all variables must be defined before usage, if some variable is not defined, the exception will be thrown. Iffalse
then all undefined variables will be treated asnull
. Default isfalse
.withShowExceptionsFromInclude(boolean showExceptionsFromInclude)
- if set totrue
then all exceptions from included templates will be thrown. Iffalse
then all exceptions from included templates will be ignored. Default istrue
.withEvaluateMode(TemplateParser.EvaluateMode evaluateMode)
- there exists two rendering modes:TemplateParser.EvaluateMode.LAZY
andTemplateParser.EvaluateMode.EAGER
. By default, thelazy
one is used. This should do the work in most cases.- In
lazy
mode the template parameters are evaluating on demand and specific properties are read from there only if they are needed. Each filter/tag trying to do its work with its own parameter object, that can be literally anything. - In
eager
the entire parameter object is converted into plain data tree structure made only from maps and lists, so tags/filters do know how to work with these kinds of objects. Special case - temporal objects, they are consumed as is.
- In
withRenderTransformer(RenderTransformer renderTransformer)
- even if most of elements (filters/tags/blocks) returns its results most cases asString
, the task of combining all those strings into a final result is a task ofliqp.RenderTransformer
implementation. The defaultliqp.RenderTransformerDefaultImpl
usesStringBuilder
for that task, so template rendering is fast. Although, you might have special needs or environment to render the results.withLocale(Locale locale)
- locale to be used for rendering. Default isLocale.ENGLISH
. Used mostly for time rendering.withDefaultTimeZone(ZoneId defaultTimeZone)
- default time zone to be used for rendering. Default isZoneId.systemDefault()
. Used mostly for time rendering.withEnvironmentMapConfigurator(Consumer<Map<String, Object>> configurator)
- if provided then this configurator is called before each template rendering. It can be used to set some global variables for all templates built with givenTemplateParser
.withSnippetsFolderName(String snippetsFolderName)
- define folder to be used for searching files byinclude
tag. Defaults depend on flavor:Flavor.LIQUID
andFlavor.LIQP
has this set tosnippets
;Flavor.JEKYLL
uses_includes
.withNameResolver(NameResolver nameResolver)
- if provided then this resolver is used for resolving names of included files. If not provided, then default resolver is used. Default resolver isliqp.antlr.LocalFSNameResolver
that usesjava.nio.file.Path
for resolving names in local file system. That can be changed to any other resolver, for example, to resolve names in classpath or in remote file system or even build templates dynamically by name.withMaxIterations(int maxIterations)
- maximum number of iterations allowed in a template. Default isInteger.MAX_VALUE
. Used to prevent infinite loops. Example use: evaluate templates from untrusted sources.withMaxSizeRenderedString(int maxSizeRenderedString)
- maximum size of rendered string. Default isInteger.MAX_VALUE
. Used to prevent out of memory errors. Example use: evaluate templates from untrusted sources.withMaxRenderTimeMillis(long maxRenderTimeMillis)
- maximum time allowed for template rendering. Default isLong.MAX_VALUE
. Used to prevent never-ending rendering. Example use: evaluate templates from untrusted sources.withMaxTemplateSizeBytes(long maxTemplateSizeBytes)
- maximum size of template. Default isLong.MAX_VALUE
. Used to prevent out of memory errors. Example use: evaluate templates from untrusted sources.withErrorMode(ErrorMode errorMode)
- error mode to be used. Default isErrorMode.STRICT
.
Let's say you want to create a custom filter, called b
, that changes a string like
*text*
to <strong>text</strong>
.
You can do that as follows:
// first create template parser with new filter
TemplateParser parser = new TemplateParser.Builder().withFilter(new Filter("b") {
@Override
public Object apply(Object value, TemplateContext context, Object... params) {
// create a string from the value
String text = super.asString(value, context);
// replace and return *...* with <strong>...</strong>
return text.replaceAll("\\*(\\w(.*?\\w)?)\\*", "<strong>$1</strong>");
}
}).build();
// use your filter
Template template = parser.parse("{{ wiki | b }}");
String rendered = template.render("{\"wiki\" : \"Some *bold* text *in here*.\"}");
System.out.println(rendered);
/*
Some <strong>bold</strong> text <strong>in here</strong>.
*/
And to use an optional parameter in your filter, do something like this:
// first create template parser with your filter
TemplateParser parser = new TemplateParser.Builder().withFilter(new Filter("repeat"){
@Override
public Object apply(Object value, TemplateContext context, Object... params) {
// get the text of the value
String text = super.asString(value, context);
// check if an optional parameter is provided
int times = params.length == 0 ? 1 : super.asNumber(params[0]).intValue();
StringBuilder builder = new StringBuilder();
while(times-- > 0) {
builder.append(text);
}
return builder.toString();
}
}).build();
// use your filter
Template template = parser.parse("{{ 'a' | repeat }}\n{{ 'b' | repeat:5 }}");
String rendered = template.render();
System.out.println(rendered);
/*
a
bbbbb
*/
You can use an array (or list) as well, and can also return a numerical value:
TemplateParser parser = new TemplateParser.Builder().withFilter((new Filter("sum"){
@Override
public Object apply(Object value, TemplateContext context, Object... params) {
Object[] numbers = super.asArray(value, context);
double sum = 0;
for(Object obj : numbers) {
sum += super.asNumber(obj).doubleValue();
}
return sum;
}
})).build();
Template template = parser.parse("{{ numbers | sum }}");
String rendered = template.render("{\"numbers\" : [1, 2, 3, 4, 5]}");
System.out.println(rendered);
/*
15.0
*/
In short, override one of the apply()
methods of the Filter
class to create your own custom filter behavior.
Both blocks and tags are kinds of insertions. Literally: Block extends Insertion
and Tag extends Insertion
. Class Insertion
used as basic abstraction for parser.
Below is defined the difference between tags and blocks.
Tag is a simple insertion in the template that will be processed and the result of it will be replaced in output, if any. Example is include
tag:
See these data: {% include data.liquid %}
Another example is assign
tag:
{% assign name='Joe' %}
Hello {{name}}!
It has no input but still is an insertion.
Block is a kind of insertions that wraps some text and/or other blocks or tags and performs some operations on the given input. Blocks have opening and closing tags. Example of block is if
:
{% if user %} Hello {{ user.name }} {% endif %}"
where {% if user %}
is opening tag and {% endif %}
is closing one. The user
in this sample is just a parameter for given block.
Let's say you would like to create a block that makes it easy to loop for a fixed number of times executing a block of Liquid code.
Here's a way to create, and use, such a custom loop
block:
TemplateParser parser = new TemplateParser.Builder().withBlock(new Block("loop"){
@Override
public Object render(TemplateContext context, LNode... nodes) {
int n = super.asNumber(nodes[0].render(context)).intValue();
LNode block = nodes[1];
StringBuilder builder = new StringBuilder();
while(n-- > 0) {
builder.append(super.asString(block.render(context), context));
}
return builder.toString();
}
}).build();
Template template = parser.parse("{% loop 5 %}looping!\n{% endloop %}");
String rendered = template.render();
System.out.println(rendered);
/*
looping!
looping!
looping!
looping!
looping!
*/
For registering custom Tags there exists equivalent Builder.withTag
function.
Use Maven 3.5.0 and run build with
mvn clean install
Release process into the Central Repository is performed with
mvn release:clean release:prepare release:perform -P ossrh-release
Make sure having in ~/.m2/settings.xml
this config(with your values):
<settings>
<servers>
<server>
<id>ossrh</id>
<username>MY_OSSRH_USERNAME</username>
<password>MY_OSSRH_PASSWORD</password>
</server>
</servers>
<profiles>
<profile>
<id>ossrh-release</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg2</gpg.executable>
<gpg.passphrase>GPG_PRIVATE_KEY_PASSWORD</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
After executing this go to https://oss.sonatype.org/index.html#stagingRepositories, ensure all is OK, after you can "Close" the staging for promoting to the realease and after do "Release".