StringTemplate - MTDdk/jawn GitHub Wiki
Configuring the default template engine is done by creating a app.config.TemplateConfig
and let it extend AbstractStringTemplateConfig:
package app.config;
public class TemplateConfig extends AbstractStringTemplateConfig {
@Override
public void init(StringTemplateConfiguration config) {
config.registerRenderer(Date.class, new DateRenderer());
}
}StringTemplate has various configuration and customisation options.
Most of these are implementations of Model Adaptors
and Renderers, which are
exposed through the public void init(StringTemplateConfiguration config), when extending
AbstractStringTemplateConfig.
StringTemplate will most often just call the toString() on a type it does not know of, and
if something special needs to be done for a given class type, you can write your own renderer,
like the DateRenderer in the example, which lets you set a format for a given keyword of the
type Date:
<div class="modal-body">
<h5>$it.date; format="dd. MMMM YYYY"$</h5>
<h3 class="news-headline">$it.headline$</h3>
</div>public class StringAdaptor implements ModelAdaptor {
@Override
public Object getProperty(
Interpreter interp,
ST self,
Object o,
Object property,
String propertyName)
throws STNoSuchPropertyException {
switch (propertyName) {
case "asform":
return String.format(
"<form><input type=\"hidden\" value=\"%s\"></form>",
o.toString());
}
throw new STNoSuchPropertyException(null, property, propertyName);
}
}These extend your model by adding virtual properties to be used in the template:
<div class="modal-body">
$it.headline.asform$
</div>Adaptors are registered like the renderers in StringTemplateConfiguration.
Per default StringTemplate, in jawn, is configured to use dollar signs as keyword delimiters. This, however, can also be altered.
public void init(StringTemplateConfiguration config) {
config.setDelimiterStart('<');
config.setDelimiterEnd('>');
}At this point, only char can be used, but this will change in the futrue.