@Test public void applyTest() throws RecognitionException { Template template = liquid.parse("{{values | last}}"); String rendered = String.valueOf(template.render("{\"values\" : [\"Mu\", \"foo\", \"bar\"]}")); assertThat(rendered, is("bar")); }
@Test public void testCustomTag() throws RecognitionException { Tag.registerTag( new Tag("twice") { @Override public Object render(Context context, LNode... nodes) { Double number = super.asNumber(nodes[0].render(context)).doubleValue(); return number * 2; } }); Template template = liquid.parse("{% twice 10 %}"); String rendered = String.valueOf(template.render()); assertThat(rendered, is("20.0")); }
@Test public void testCustomTagBlock() throws RecognitionException { Tag.registerTag( new Tag("twice") { @Override public Object render(Context context, LNode... nodes) { LNode blockNode = nodes[nodes.length - 1]; String blockValue = super.asString(blockNode.render(context)); return blockValue + " " + blockValue; } }); Template template = liquid.parse("{% twice %}abc{% endtwice %}"); String rendered = String.valueOf(template.render()); assertThat(rendered, is("abc abc")); }