|
| 1 | +package com.stubbornjava.common; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.JsonNode; |
| 10 | +import com.github.jknack.handlebars.Context; |
| 11 | +import com.github.jknack.handlebars.Handlebars; |
| 12 | +import com.github.jknack.handlebars.Helper; |
| 13 | +import com.github.jknack.handlebars.JsonNodeValueResolver; |
| 14 | +import com.github.jknack.handlebars.Template; |
| 15 | +import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache; |
| 16 | +import com.github.jknack.handlebars.io.ClassPathTemplateLoader; |
| 17 | +import com.github.jknack.handlebars.io.TemplateLoader; |
| 18 | +import com.google.common.collect.Lists; |
| 19 | +import com.googlecode.htmlcompressor.compressor.HtmlCompressor; |
| 20 | +// {{start:templating}} |
| 21 | +public class Templating { |
| 22 | + private static final Logger logger = LoggerFactory.getLogger(Templating.class); |
| 23 | + |
| 24 | + // Once again using static for convenience use your own DI method. |
| 25 | + private static final Templating DEFAULT; |
| 26 | + static { |
| 27 | + Templating.Builder builder = |
| 28 | + new Templating.Builder() |
| 29 | + .withResourceLoaders() |
| 30 | + .withHelper("dateFormat", TemplateHelpers::dateFormat); |
| 31 | + // Don't cache locally, makes development annoying |
| 32 | + if (Env.LOCAL != Env.get()) { |
| 33 | + builder.withCaching(); |
| 34 | + } |
| 35 | + DEFAULT = builder.build(); |
| 36 | + } |
| 37 | + |
| 38 | + public static Templating instance() { |
| 39 | + return DEFAULT; |
| 40 | + } |
| 41 | + |
| 42 | + private final Handlebars handlebars; |
| 43 | + private final HtmlCompressor compressor = new HtmlCompressor(); |
| 44 | + Templating(Handlebars handlebars) { |
| 45 | + this.handlebars = handlebars; |
| 46 | + } |
| 47 | + |
| 48 | + public String renderHtmlTemplate(String templateName, Object data) { |
| 49 | + String response = renderTemplate(templateName, data); |
| 50 | + return compressor.compress(response); |
| 51 | + } |
| 52 | + |
| 53 | + public String renderTemplate(String templateName, Object data) { |
| 54 | + Template template; |
| 55 | + try { |
| 56 | + template = handlebars.compile(templateName); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new RuntimeException(e); |
| 59 | + } |
| 60 | + return render(template, data); |
| 61 | + } |
| 62 | + |
| 63 | + public String renderRawHtmlTemplate(String rawTemplate, Object data) { |
| 64 | + String response = rednerRawTemplate(rawTemplate, data); |
| 65 | + return compressor.compress(response); |
| 66 | + } |
| 67 | + |
| 68 | + public String rednerRawTemplate(String rawTemplate, Object data) { |
| 69 | + Template template; |
| 70 | + try { |
| 71 | + template = handlebars.compileInline(rawTemplate); |
| 72 | + } catch (IOException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + return render(template, data); |
| 76 | + } |
| 77 | + |
| 78 | + private String render(Template template, Object data) { |
| 79 | + Context context = null; |
| 80 | + try { |
| 81 | + JsonNode node = Json.serializer().nodeFromObject(data); |
| 82 | + // Very useful for debugging templates |
| 83 | + if (logger.isDebugEnabled()) { |
| 84 | + logger.debug("rendering template " + template.filename() + "\n" + Json.serializer().toPrettyString(node)); |
| 85 | + } |
| 86 | + context = Context.newBuilder(node) |
| 87 | + .resolver(JsonNodeValueResolver.INSTANCE) |
| 88 | + .build(); |
| 89 | + return template.apply(context); |
| 90 | + } catch (IOException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } finally { |
| 93 | + if (null != context) { |
| 94 | + context.destroy(); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + static class Builder { |
| 100 | + private final Handlebars handlebars = new Handlebars(); |
| 101 | + private final List<TemplateLoader> loaders = Lists.newArrayList(); |
| 102 | + public Builder() { |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public Builder withResourceLoaders() { |
| 107 | + logger.debug("using resource loaders"); |
| 108 | + loaders.add(new ClassPathTemplateLoader()); |
| 109 | + loaders.add(new ClassPathTemplateLoader(TemplateLoader.DEFAULT_PREFIX, ".sql")); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder withCaching() { |
| 114 | + logger.debug("Using caching handlebars"); |
| 115 | + handlebars.with(new ConcurrentMapTemplateCache()); |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public <T> Builder withHelper(String helperName, Helper<T> helper) { |
| 120 | + logger.debug("using template helper {}" , helperName); |
| 121 | + handlebars.registerHelper(helperName, helper); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public Templating build() { |
| 126 | + return new Templating(handlebars); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +// {{end:templating}} |
0 commit comments