-
Notifications
You must be signed in to change notification settings - Fork 1
Usage
Once dependencies has been set, usage is easy.
First of all, should to configure. This library is auto-configurable with spring boot.
If not, you should to import the configuration class cat.albirar.template.engine.configuration.TemplateEngineConfiguration.
The templates are files that should to be placed in the classpath, as a resource.
The template engine is a Spring Bean dependency, resolved through autowired annotation:
// ...
@Autowired
private ITemplateEngine templateEngine;
// ...By default, a template engine implementation is provided. This engine works with Thymeleaf language with Spring Beans extension.
Remember to use the interface, so concrete implementations can be selected through configuration.
A template definition is a general applicable template that should to be instantiated in order to use them.
A cat.albirar.template.engine.models.TemplateDefinitionBean describe the invariable template aspects, like name, template resource classpath, content type and charset:
TemplateDefinitionBean tDefinition;
tDefinition = TemplateDefinitionBean.builder()
.name("Template1")
.contentType(EContentType.HTML)
.template("classpath:/cat/albirar/template/engine/templates/simpleTemplate.html")
.build()
;Once the definition is created, a cat.albirar.template.engine.models.TemplateInstanceBean can be created from definition and set the specific aspects of each rendering, like variables and messages.
TemplateInstanceBean tInstance;
Map<String, Object> vars;
ResourceBundleMessageSource messages;
vars = new TreeMap<>();
vars.put("title", "The first template title");
vars.put("users", Arrays.asList(VARS_USERS));
messages = new ResourceBundleMessageSource();
messages.addBasenames("cat/albirar/template/engine/messages/template1");
tInstance = TemplateInstanceBean.buildInstance(tDefinition)
.locale(new Locale("ca"))
.variables(vars)
.messages(messages)
.build()
;With the template instance, you can do the rendering:
String rendered;
rendered = templateEngine.renderTemplate(tInstance);