Class MarkupTemplateModule

  • All Implemented Interfaces:

    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 of MarkupTemplate can be created using one of the Groovy.groovyMarkupTemplate(java.util.Map, String, String) static methods.

    By default templates are looked up in the templates directory of the application root. So groovyMarkupTemplate("my/template/path.gtpl") maps to tempaltes/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 to text/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