|
| 1 | +package org.jooby.thymeleaf; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | + |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.function.BiConsumer; |
| 7 | +import java.util.function.Consumer; |
| 8 | + |
| 9 | +import org.jooby.Env; |
| 10 | +import org.jooby.Jooby; |
| 11 | +import org.jooby.Renderer; |
| 12 | +import org.jooby.View; |
| 13 | +import org.thymeleaf.ITemplateEngine; |
| 14 | +import org.thymeleaf.TemplateEngine; |
| 15 | +import org.thymeleaf.templatemode.TemplateMode; |
| 16 | +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; |
| 17 | + |
| 18 | +import com.google.inject.Binder; |
| 19 | +import com.google.inject.multibindings.Multibinder; |
| 20 | +import com.typesafe.config.Config; |
| 21 | + |
| 22 | +/** |
| 23 | + * <h1>thymeleaf</h1> |
| 24 | + * <p> |
| 25 | + * <a href="http://www.thymeleaf.org">Thymeleaf</a> is a modern server-side Java template engine for |
| 26 | + * both web and standalone environments. |
| 27 | + * </p> |
| 28 | + * |
| 29 | + * <h2>exports</h2> |
| 30 | + * <ul> |
| 31 | + * <li>{@link TemplateEngine}</li> |
| 32 | + * <li>{@link View.Engine}</li> |
| 33 | + * </ul> |
| 34 | + * |
| 35 | + * <h2>usage</h2> |
| 36 | + * |
| 37 | + * <pre>{@code |
| 38 | + * { |
| 39 | + * use(new Thl()); |
| 40 | + * |
| 41 | + * get("/", () -> { |
| 42 | + * return Results.html("index") |
| 43 | + * .put("model", new MyModel()); |
| 44 | + * }); |
| 45 | + * |
| 46 | + * // Or Thymeleaf API: |
| 47 | + * get("/thymeleaf-api", () -> { |
| 48 | + * TemplateEngine engine = require(TemplateEngine.class); |
| 49 | + * engine.processs("template", ...); |
| 50 | + * }); |
| 51 | + * } |
| 52 | + * }</pre> |
| 53 | + * |
| 54 | + * <p> |
| 55 | + * Templates are loaded from root of classpath: <code>/</code> and must end with: <code>.html</code> |
| 56 | + * file extension. Example: |
| 57 | + * </p> |
| 58 | + * |
| 59 | + * <p> |
| 60 | + * public/index.html |
| 61 | + * </p> |
| 62 | + * <pre>{@code |
| 63 | + * <!DOCTYPE html> |
| 64 | + * <html xmlns:th="http://www.thymeleaf.org"> |
| 65 | + * <body> |
| 66 | + * <p> |
| 67 | + * Hello <span th:text="${model.name}">World</span>!!! |
| 68 | + * </p> |
| 69 | + * </body> |
| 70 | + * </html> |
| 71 | + * }</pre> |
| 72 | + * |
| 73 | + * <h2>template loader</h2> |
| 74 | + * <p> |
| 75 | + * Templates are loaded from the <code>root</code> of classpath and must end with |
| 76 | + * <code>.html</code>. You can change the default template location and/or extensions: |
| 77 | + * </p> |
| 78 | + * |
| 79 | + * <pre>{@code |
| 80 | + * { |
| 81 | + * use(new Thl("templates", ".thl")); |
| 82 | + * } |
| 83 | + * }</pre> |
| 84 | + * |
| 85 | + * <h2>request locals</h2> |
| 86 | + * <p> |
| 87 | + * A template engine has access to request locals (a.k.a attributes). Here is an example: |
| 88 | + * </p> |
| 89 | + * |
| 90 | + * <pre>{@code |
| 91 | + * { |
| 92 | + * use(new Thl()); |
| 93 | + * |
| 94 | + * get("*", req -> { |
| 95 | + * req.set("foo", bar); |
| 96 | + * }); |
| 97 | + * } |
| 98 | + * }</pre> |
| 99 | + * |
| 100 | + * <p> |
| 101 | + * Then from template: |
| 102 | + * </p> |
| 103 | + * |
| 104 | + * <pre>{@code |
| 105 | + * <span th:text="${who}">World</span> |
| 106 | + * }</pre> |
| 107 | + * |
| 108 | + * <h2>template cache</h2> |
| 109 | + * <p> |
| 110 | + * Cache is OFF when <code>env=dev</code> (useful for template reloading), otherwise is ON. |
| 111 | + * </p> |
| 112 | + * |
| 113 | + * <h2>advanced configuration</h2> |
| 114 | + * <p> |
| 115 | + * Advanced configuration if provided by callback: |
| 116 | + * </p> |
| 117 | + * |
| 118 | + * <pre>{@code |
| 119 | + * { |
| 120 | + * use(new Thl().doWith(engine -> { |
| 121 | + * engine.addDialect(...); |
| 122 | + * })); |
| 123 | + * } |
| 124 | + * }</pre> |
| 125 | + * |
| 126 | + * @author edgar |
| 127 | + */ |
| 128 | +public class Thl implements Jooby.Module { |
| 129 | + |
| 130 | + private final String prefix; |
| 131 | + |
| 132 | + private final String suffix; |
| 133 | + |
| 134 | + private BiConsumer<TemplateEngine, Config> callback; |
| 135 | + |
| 136 | + /** |
| 137 | + * Creates a new thymeleaf module. |
| 138 | + * |
| 139 | + * @param prefix Template prefix. |
| 140 | + * @param suffix Template suffix. |
| 141 | + */ |
| 142 | + public Thl(final String prefix, final String suffix) { |
| 143 | + this.prefix = Objects.requireNonNull(prefix, "Prefix required."); |
| 144 | + this.suffix = Objects.requireNonNull(suffix, "Suffix required."); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates a new thymeleaf module. |
| 149 | + */ |
| 150 | + public Thl() { |
| 151 | + this("/", ".html"); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Set a configuration callback. |
| 156 | + * |
| 157 | + * <pre>{@code |
| 158 | + * { |
| 159 | + * use(new Thl().doWith(engine -> { |
| 160 | + * ... |
| 161 | + * })); |
| 162 | + * } |
| 163 | + * }</pre> |
| 164 | + * |
| 165 | + * @param callback Callback. |
| 166 | + * @return This module. |
| 167 | + */ |
| 168 | + public Thl doWith(final Consumer<TemplateEngine> callback) { |
| 169 | + requireNonNull(callback, "Callback required."); |
| 170 | + return doWith((e, c) -> callback.accept(e)); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Set a configuration callback. |
| 175 | + * |
| 176 | + * <pre>{@code |
| 177 | + * { |
| 178 | + * use(new Thl().doWith(engine -> { |
| 179 | + * ... |
| 180 | + * })); |
| 181 | + * } |
| 182 | + * }</pre> |
| 183 | + * |
| 184 | + * @param callback Callback. |
| 185 | + * @return This module. |
| 186 | + */ |
| 187 | + public Thl doWith(final BiConsumer<TemplateEngine, Config> callback) { |
| 188 | + this.callback = requireNonNull(callback, "Callback required."); |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public void configure(final Env env, final Config conf, final Binder binder) throws Throwable { |
| 194 | + ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); |
| 195 | + boolean cacheable = !env.name().equals("dev"); |
| 196 | + /** Defaults: */ |
| 197 | + resolver.setCacheable(cacheable); |
| 198 | + resolver.setPrefix(prefix); |
| 199 | + resolver.setSuffix(suffix); |
| 200 | + resolver.setTemplateMode(TemplateMode.HTML); |
| 201 | + |
| 202 | + TemplateEngine engine = new TemplateEngine(); |
| 203 | + engine.setTemplateResolver(resolver); |
| 204 | + |
| 205 | + if (callback != null) { |
| 206 | + callback.accept(engine, conf); |
| 207 | + } |
| 208 | + |
| 209 | + binder.bind(TemplateEngine.class).toInstance(engine); |
| 210 | + binder.bind(ITemplateEngine.class).toInstance(engine); |
| 211 | + |
| 212 | + Multibinder.newSetBinder(binder, Renderer.class) |
| 213 | + .addBinding() |
| 214 | + .toInstance(new ThlEngine(engine, env)); |
| 215 | + } |
| 216 | +} |
0 commit comments