Template engines - MTDdk/jawn GitHub Wiki

jawn uses the StringTemplate as the HTML renderer by default, but it is extremely easy to switch renderer or even write your own and plug it in.

TemplateConfig

If the standard use of StringTemplate is undesired, make sure your other renderer is in the classpath of your application, and simply let your app.config.TemplateConfig extend the subclass of AbstractTemplateConfig of your wanted framework.

That is all there is to it.

The framework will automatically use the renderer associated with the AbstractTemplateConfig.

Using StringTemplate:

package app.config;
public class TemplateConfig extends AbstractStringTemplateConfig {
    @Override
    public void init(StringTemplateConfiguration config) {
        config.registerRenderer(Date.class, new DateRenderer());
    }
}

Using FreeMarker:

package app.config;
public class TemplateConfig extends AbstractFreeMarkerConfig {
    @Override
    public void init(Configuration config) {
    	config.setNumberFormat("0.##");
    }
}

Create your own

If you are not satisfied with the options available, it is possible to make your own implementation of the interface TemplateManager and the AbstractTemplateConfig, and let the framework use it.

All you need to do is to make sure both implementations are within the same package - the framework then finds the manager in the same location, it does not care if any other classes like helpers or configurations reside in the package as well.

TemplateManager

Most notably you need to implement the various merge(..) methods. One simple way is to make them chain each other with (what you consider to be) standard values.

Besides this, your manager needs a public constructor, which can either take its own implementation of AbstractTemplateConfig as its sole argument, or a default no-argument constructor.

AbstractTemplateConfig

This exposes a configuration to the user to configure the renderer.

The most simple implementation is:

public abstract class AbstractStringTemplateConfig 
	extends AbstractTemplateConfig<StringTemplateConfiguration> {}

It is possible to expand the class with further functionality if needed. For instance some extra methods that let the user register some custom tags, which has their dependencies defined in the Injector sent to public void inject(Injector injector).

The user then just extends this as app.config.TemplateConfig, and the renderer is automatically used by the framework.