quick start.
Getting started with templar:
Download a release from GitHub and get started. (Make sure that you grab the -all
classifier)
In a very small nutshell
- Create a new templarContext
- Add stuff to it
- Create a parser (and pass in the templar file)
- Render the content
This example was taken from sab-spot-comment:
TemplarContext templarContext = new TemplarContext();
templarContext.add("setupManager", SetupManager.INSTANCE);
templarContext.add("dummySuccessComment", new Download("Dummy Success Download", "http://www.example.com/", "SAB_NZB_ID", "", System.currentTimeMillis(), "GUID", false).getComment());
templarContext.add("dummyFailedComment", new Download("Dummy Failed Download", "http://www.example.com/", "SAB_NZB_ID", "Unpacking failed, archive requires a password", System.currentTimeMillis(), "GUID", false).getComment());
templarContext.add("dummyIgnoredDownload", new Download("Dummy Ignored Download", "http://www.example.com/", "SAB_NZB_ID", "Unpacking failed, archive requires a password", System.currentTimeMillis(), "GUID", true));
try {
Parser parser = new Parser(this.getClass().getResourceAsStream("/synapticloop/ssc/template/admin.templar"));
return(HttpUtils.okResponse(NanoHTTPD.MIME_HTML, parser.render(templarContext)));
} catch (ParseException pex) {
pex.printStackTrace();
return(HttpUtils.internalServerErrorResponse());
} catch (RenderException rex) {
rex.printStackTrace();
return(HttpUtils.internalServerErrorResponse());
}
1. Creating the TemplarContext
TemplarContext templarContext = new TemplarContext();
This creates a default context, with a default configuration, meaning
- You must explicitly put in new lines
- You must explicitly put in tab characters
- Whitespace is ignored
These are the defaults and the output can be seen by viewing source of these pages - which should be mostlyon one line (more about the {pre ... pre}
syntax later).
If you wish to over-ride this default behaviour, you will need to instantiate a TemplarContext
witha passed in TemplarConfiguration
. e.g.:
TemplarConfiguration templarConfiguration = new TemplarConfiguration();
templarConfiguration.setExplicitNewLines(false);
templarConfiguration.setExplicitTabs(false);
templarConfiguration.setIgnoreWhitespace(false);
or
TemplarConfiguration templarConfiguration = new TemplarConfiguration(false, false, false);
And then:
TemplarContext templarContext = new TemplarContext(templarConfiguration);
2. Adding objects to the templarContext
Very simply
templarContext.add(key, value)
The templar context is simply a key:value hash map.
3. Creating a parser object
Very simply
try {
Parser parser = new Parser(this.getClass().getResourceAsStream("/synapticloop/ssc/template/admin.templar"));
} catch (ParseException pex) {
pex.printStackTrace();
}
4. Rendering a parser object
Very simply
try {
String render = parser.render(templarContext);
// do something with the rendered content...
} catch (RenderException rex) {
rex.printStackTrace();
}