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.handling.*; import ratpack.guice.*; import ratpack.func.Action; import ratpack.launch.*; import ratpack.thymeleaf.ThymeleafModule; import static ratpack.thymeleaf.Template.thymeleafTemplate; class MyHandler implements Handler { void handle(final Context context) { context.render(thymeleafTemplate("my/template/path", key: "it works!")); } } class ModuleBootstrap implements Action<ModuleRegistry> { public void execute(ModuleRegistry modules) { modules.register(new ThymeleafModule()); } } LaunchConfig launchConfig = LaunchConfigBuilder.baseDir(new File("appRoot")) .build(new HandlerFactory() { public Handler create(LaunchConfig launchConfig) { return Guice.handler(launchConfig, new ModuleBootstrap(), new Action<Chain>() { public void execute(Chain chain) { chain.handler(chain.getRegistry().get(MyHandler.class)); } }); } });Example usage: (Groovy DSL)
import ratpack.thymeleaf.ThymeleafModule import static ratpack.thymeleaf.Template.thymeleafTemplate import static ratpack.groovy.Groovy.ratpack ratpack { modules { register new ThymeleafModule() } handlers { get { render thymeleafTemplate('my/template/path', key: 'it works!') } } }
To register dialects, use Guice Multibindings to bind an implementation of IDialect
in a module.
import com.google.inject.AbstractModule import com.google.inject.multibindings.Multibinder import org.thymeleaf.Arguments import org.thymeleaf.dialect.AbstractDialect import org.thymeleaf.dialect.IDialect import org.thymeleaf.dom.Element import org.thymeleaf.processor.IProcessor import org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor import ratpack.thymeleaf.ThymeleafModule import static ratpack.groovy.Groovy.ratpack import static ratpack.thymeleaf.Template.thymeleafTemplate class SayToAttrProcessor extends AbstractTextChildModifierAttrProcessor { int precedence = 10000 SayToAttrProcessor() { super('sayto') } protected String getText(Arguments arguments, Element element, String attributeName) { return "Hello, ${element.getAttributeValue(attributeName)}!" } } class HelloDialect extends AbstractDialect { String prefix = 'hello' Set<IProcessor> processors = [new SayToAttrProcessor()] as Set } class HelloDialectModule extends AbstractModule { protected void configure() { Multibinder.newSetBinder(binder(), IDialect).addBinding().to(HelloDialect) } } ratpack { modules { register new ThymeleafModule() register new HelloDialectModule() } handlers { get { render thymeleafTemplate('my/template/path', key: 'it works!') } } }
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, 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