public class ThymeleafModule extends AbstractModule
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
other.thymeleaf.templatesPrefix and other.thymeleaf.templatesSuffix configuration properties.
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;
public class Example {
public static void main(String... args) {
Path baseDir = BaseDirBuilder.tmpDir().build(builder ->
builder.file("thymeleaf/myTemplate.html", "<span th:text=\"${key}\"/>")
);
EmbeddedApp.fromHandlerFactory(baseDir, launchConfig ->
Guice.builder(launchConfig)
.bindings(b -> b.add(new ThymeleafModule()))
.build(chain -> chain
.get(ctx -> ctx.render(thymeleafTemplate("myTemplate", m -> m.put("key", "Hello Ratpack!"))))
)
).test(httpClient -> {
assert httpClient.getText().equals("<span>Hello Ratpack!</span>");
});
}
}
To register dialects, use Guice Multibindings to bind an implementation of IDialect in a module.
| 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) |
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBindingpublic 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