This example is a toy implementation of the StringTemplate template engine. It uses a bytecode interpreter and disassembler pulled directly from:
I'm in the process of rebuilding ST from the ground up. The previous version works well, but it's hard for me to maintain because it uses a tree-based interpreter. Further, it uses v2 ANTLR not v3. In order to remove the dependencies in ST and in v3 itself on ANTLR v2, I'm rebuilding both (though only the front end of ANTLR). The functionality will be effectively the same, with some slight differences in syntax. Lots of little things cleaned up and so on.
Along they way, I've created a very simple bytecode compiler and interpreter that handles the canonical operations:
- attribute reference: <name>, <user.id>
- template inclusion: <searchbox(args)>
- map template to attribute(s): <names:bold:italics>
- conditional inclusion: <if>foo<elseif>bar<else>ick<endif>
It seems to work and the code (1900 lines) seems pretty doggone clean to me. It's only when you start dealing with separators and auto indentation and anchoring output values, that the code becomes difficult to understand.
Differences from ST v3
- ST not StringTemplate
- STGroup not StringTemplateGroup
- ST.add() not setAttribute()
- render() not toString() used to render templates to text
You can download the Toy ST tarball.
File TestCoreBasics.java has tests for the interpreter like this:
TestCompiler.java has tests for the compiler: