Class MarkupTemplateModule
- java.lang.Object
-
- com.google.inject.AbstractModule
-
- ratpack.guice.ConfigurableModule<MarkupTemplateModule.Config>
-
- ratpack.groovy.template.MarkupTemplateModule
-
- All Implemented Interfaces:
com.google.inject.Module
public class MarkupTemplateModule extends ConfigurableModule<MarkupTemplateModule.Config>
An extension module that provides support for the Groovy markup template engine.To use it one has to register the module and then render
MarkupTemplate
instances. Instances ofMarkupTemplate
can be created using one of theGroovy.groovyMarkupTemplate(java.util.Map, String, String)
static methods.By default templates are looked up in the
templates
directory of the application root. SogroovyMarkupTemplate("my/template/path.gtpl")
maps totempaltes/my/template/path.gtpl
in the application root directory.The template engine can be configured using the
template configuration
. In particular, it is possible to configure things like automatic indentation.Response content type can be manually specified, i.e.
groovyMarkupTemplate("template.gtpl", model, "text/html")
if not specified will default totext/html
.import ratpack.groovy.template.MarkupTemplateModule; import ratpack.guice.Guice; import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import java.nio.file.Path; import static ratpack.groovy.Groovy.groovyMarkupTemplate; import static org.junit.jupiter.api.Assertions.*; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("templates/myTemplate.gtpl", "html { body { p(value) } }"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .registry(Guice.registry(b -> b.module(MarkupTemplateModule.class))) .handlers(chain -> chain .get(ctx -> ctx.render(groovyMarkupTemplate("myTemplate.gtpl", m -> m.put("value", "hello!")))) ) ).test(httpClient -> { assertEquals("<html><body><p>hello!</p></body></html>", httpClient.get().getBody().getText()); }); }); } }
- See Also:
- Groovy Markup Template Engine
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MarkupTemplateModule.Config
-
Constructor Summary
Constructors Constructor Description MarkupTemplateModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
configure()
protected void
defaultConfig(ServerConfig serverConfig, MarkupTemplateModule.Config config)
Hook for applying any default configuration to the configuration object created byConfigurableModule.createConfig(ServerConfig)
.-
Methods inherited from class ratpack.guice.ConfigurableModule
configure, createConfig, setConfig
-
Methods inherited from class com.google.inject.AbstractModule
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
-
-
-
-
Method Detail
-
configure
protected void configure()
- Overrides:
configure
in classcom.google.inject.AbstractModule
-
defaultConfig
protected void defaultConfig(ServerConfig serverConfig, MarkupTemplateModule.Config config)
Description copied from class:ConfigurableModule
Hook for applying any default configuration to the configuration object created byConfigurableModule.createConfig(ServerConfig)
.This can be used if it's not possible to apply the configuration in the constructor.
- Overrides:
defaultConfig
in classConfigurableModule<MarkupTemplateModule.Config>
- Parameters:
serverConfig
- the application server configconfig
- the config object
-
-