public class ThymeleafModule extends ConfigurableModule<ThymeleafModule.Config>
To use it one has to register the module and then render Template
instances.
Instances of Template
can be created using one of the
Template.thymeleafTemplate(java.util.Map, String, String)
static methods.
By default templates are looked up in the thymeleaf
directory of the application root with a .html
suffix.
So thymeleafTemplate("my/template/path")
maps to thymeleaf/my/template/path.html
in the application root directory.
This can be configured using setTemplatesPrefix(String)
and setTemplatesSuffix(String)
as well as configuration of
ThymeleafModule.Config.templatesPrefix(String)
and ThymeleafModule.Config.templateSuffix(String)
.
Response content type can be manually specified, i.e. thymeleafTemplate("template", model, "text/html")
if
not specified will default to text/html
.
import ratpack.guice.Guice;
import ratpack.test.embed.BaseDirBuilder;
import ratpack.test.embed.EmbeddedApp;
import ratpack.thymeleaf.ThymeleafModule;
import java.nio.file.Path;
import static ratpack.thymeleaf.Template.thymeleafTemplate;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
Path baseDir = BaseDirBuilder.tmpDir().build(builder ->
builder.file("thymeleaf/myTemplate.html", "<span th:text=\"${key}\"/>")
);
EmbeddedApp.of(baseDir, s -> s
.registry(Guice.registry(b -> b.add(new ThymeleafModule())))
.handlers(chain -> chain
.get(ctx -> ctx.render(thymeleafTemplate("myTemplate", m -> m.put("key", "Hello Ratpack!"))))
)
).test(httpClient -> {
assertEquals("<span>Hello Ratpack!</span>", httpClient.getText());
});
}
}
To register dialects, use Guice Multibindings to bind an implementation of IDialect
in a module.
Modifier and Type | Class and Description |
---|---|
static class |
ThymeleafModule.Config
The configuration object for
ThymeleafModule . |
Constructor and Description |
---|
ThymeleafModule() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure() |
Integer |
getTemplatesCacheSize() |
String |
getTemplatesMode() |
String |
getTemplatesPrefix() |
String |
getTemplatesSuffix() |
void |
setTemplatesCacheSize(Integer templatesCacheSize) |
void |
setTemplatesMode(String templatesMode) |
void |
setTemplatesPrefix(String templatesPrefix) |
void |
setTemplatesSuffix(String templatesSuffix) |
configure, createConfig, defaultConfig, setConfig
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
public String getTemplatesMode()
public void setTemplatesMode(String templatesMode)
public String getTemplatesPrefix()
public void setTemplatesPrefix(String templatesPrefix)
public String getTemplatesSuffix()
public void setTemplatesSuffix(String templatesSuffix)
public Integer getTemplatesCacheSize()
public void setTemplatesCacheSize(Integer templatesCacheSize)
protected void configure()
configure
in class AbstractModule