StringTemplate for JPublish
A couple of weeks ago I stumbled on a few articles about the StringTemplate framework and then, digging further, I started to be attracted by the idea of using StringTemplate for rendering pages in JPublish.
I also needed to be able to learn to use StringTemplate and this is how I found few more very good blog articles at Hardlikesoftware.
In order to learn something, you need to start using it, or at least this is the principle that guides me, and this is how the StringTemplateViewRenderer was born :)
Now you can use StringTemplate to design your web site with JPublish. Is very simple, and to convince you, below are few hints to get you started.
- get JPublish (optional but good to have it handy)
- download a very simple StringTemplate based JPublish web demo application from here.
- in your jpublish.xml of the application you downloaded before you'll find the definition for the the StringTemplateViewRenderer:
<!--StringTemplate Viewer-->For integrating ST with JPublish I am using the Repository objects in JPublish which represent an abstract layer above the file system. Then for every Repository you define in jpublish.xml I am creating a new StringTemplateGroup with the name of the repository and the template root directory being that one of the Repository.
<view-renderer classname="org.jpublish.view.stringtemplate.StringTemplateViewRenderer">
<!--<lexer class="org.antlr.stringtemplate.language.AngleBracketTemplateLexer"/>-->
<lexer class="org.antlr.stringtemplate.language.DefaultTemplateLexer"/>
<!-- todo: <error-listener class=""/>-->
<refresh-interval>15</refresh-interval>
</view-renderer>
So for example, for every JPublish repositories defined, let's say you define the fs_repository like this:
<repository name="fs_repository" classname="org.jpublish.repository.filesystem.FileSystemRepository">when the StringTemplateViewRenderer is initialized, a StringTemplateGroup will be defined for every repository:
<root>content</root>
<cache>default</cache>
</repository>
...For the repository defined above, a new StringTemplateGroup called "fs_repository" will be created as having the template root to the folder 'content' in your web application and the lexer defined earlier in jpublish.xml; org.antlr.stringtemplate.language.DefaultTemplateLexer respectively.
stg = new StringTemplateGroup(repository.getName(),
repository.pathToFile(EMPTY_STRING).getAbsolutePath(), lexer);
...
Then for every page requested from JPublish, a StringTemplateGroup will be asked to return a StringTemplate instance of the page your want.
Exciting enough to get you interested in learning/using StringTemplate for web development? For me sure is, and I just started to learn ST :)
Have fun!
-florin
