Commit 9f7ec9c0 by Jan Hrabal

templates

parent 13ec164f
...@@ -2,16 +2,20 @@ package com.jh.common.template; ...@@ -2,16 +2,20 @@ package com.jh.common.template;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.jh.common.template.handlebars.HandlebarsTemplateService;
public class JhTemplateConfig { public class JhTemplateConfig {
@Bean @Bean
public TemplateEngine templateEngine() { public TemplateLoader templateLoader() {
return null; return null;
} }
@Bean @Bean
public TemplateService templateService() { public TemplateService templateService() {
return null; HandlebarsTemplateService service = new HandlebarsTemplateService();
service.setTemplateLoader(templateLoader());
return service;
} }
......
...@@ -3,6 +3,6 @@ package com.jh.common.template; ...@@ -3,6 +3,6 @@ package com.jh.common.template;
public interface TemplateLoader { public interface TemplateLoader {
String loadTemplate(String templateName);
} }
package com.jh.common.template.handlebars; package com.jh.common.template.handlebars;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Writer; import java.io.Writer;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -25,6 +23,7 @@ import com.github.jknack.handlebars.context.JavaBeanValueResolver; ...@@ -25,6 +23,7 @@ import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver; import com.github.jknack.handlebars.context.MapValueResolver;
import com.jh.common.i18n.I18nContext; import com.jh.common.i18n.I18nContext;
import com.jh.common.i18n.I18nUtils; import com.jh.common.i18n.I18nUtils;
import com.jh.common.template.TemplateLoader;
import com.jh.common.template.TemplateService; import com.jh.common.template.TemplateService;
...@@ -74,6 +73,8 @@ public class HandlebarsTemplateService implements TemplateService { ...@@ -74,6 +73,8 @@ public class HandlebarsTemplateService implements TemplateService {
private Handlebars handlebars; private Handlebars handlebars;
private TemplateLoader templateLoader;
/** /**
* Initializes instance. * Initializes instance.
...@@ -157,9 +158,18 @@ public class HandlebarsTemplateService implements TemplateService { ...@@ -157,9 +158,18 @@ public class HandlebarsTemplateService implements TemplateService {
Template template = useCache ? templateCache.get(name) : null; Template template = useCache ? templateCache.get(name) : null;
//TODO read template //TODO read template
if (template == null) {
if (templateLoader == null) {
throw new IllegalStateException("No template loader set");
}
String s = templateLoader.loadTemplate(templateCode);
if (template == null) { try {
template = handlebars.compileInline(s); template = handlebars.compileInline(s);
} catch (IOException e) {
throw new RuntimeException("Cannot compile template", e);
}
if (useCache) { if (useCache) {
templateCache.put(name, template); templateCache.put(name, template);
} }
...@@ -216,7 +226,14 @@ public class HandlebarsTemplateService implements TemplateService { ...@@ -216,7 +226,14 @@ public class HandlebarsTemplateService implements TemplateService {
@Value("${global.caching:true}") @Value("${global.caching:true}")
public void setUseCache(boolean useCache) { public void setUseCache(boolean useCache) {
this.useCache = useCache; this.useCache = useCache;
}
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