Commit 9f7ec9c0 by Jan Hrabal

templates

parent 13ec164f
......@@ -2,16 +2,20 @@ package com.jh.common.template;
import org.springframework.context.annotation.Bean;
import com.jh.common.template.handlebars.HandlebarsTemplateService;
public class JhTemplateConfig {
@Bean
public TemplateEngine templateEngine() {
public TemplateLoader templateLoader() {
return null;
}
@Bean
public TemplateService templateService() {
return null;
HandlebarsTemplateService service = new HandlebarsTemplateService();
service.setTemplateLoader(templateLoader());
return service;
}
......
......@@ -3,6 +3,6 @@ package com.jh.common.template;
public interface TemplateLoader {
String loadTemplate(String templateName);
}
package com.jh.common.template.handlebars;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.text.MessageFormat;
import java.time.LocalDate;
......@@ -25,6 +23,7 @@ import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;
import com.jh.common.i18n.I18nContext;
import com.jh.common.i18n.I18nUtils;
import com.jh.common.template.TemplateLoader;
import com.jh.common.template.TemplateService;
......@@ -74,6 +73,8 @@ public class HandlebarsTemplateService implements TemplateService {
private Handlebars handlebars;
private TemplateLoader templateLoader;
/**
* Initializes instance.
......@@ -157,9 +158,18 @@ public class HandlebarsTemplateService implements TemplateService {
Template template = useCache ? templateCache.get(name) : null;
//TODO read template
if (template == null) {
if (templateLoader == null) {
throw new IllegalStateException("No template loader set");
}
String s = templateLoader.loadTemplate(templateCode);
try {
template = handlebars.compileInline(s);
} catch (IOException e) {
throw new RuntimeException("Cannot compile template", e);
}
if (useCache) {
templateCache.put(name, template);
}
......@@ -219,4 +229,11 @@ public class HandlebarsTemplateService implements TemplateService {
}
public void setTemplateLoader(TemplateLoader templateLoader) {
this.templateLoader = templateLoader;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment