public class MarkupTemplatingModule extends AbstractModule
To use it one has to register the module and then render MarkupTemplate
instances.
Instances of ratpack.groovy.markuptemplates.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.handling.*; import ratpack.guice.*; import ratpack.func.Action; import ratpack.launch.*; import ratpack.groovy.markuptemplates.MarkupTemplatingModule; import static ratpack.groovy.Groovy.groovyMarkupTemplate; class MyHandler implements Handler { void handle(final Context context) { context.render(groovyMarkupTemplate("my/template/path.gtpl", key: "it works!")); } } class Bindings implements Action<BindingsSpec> { public void execute(BindingsSpec bindings) { bindings.add(new MarkupTemplatingModule()); } } LaunchConfig launchConfig = LaunchConfigBuilder.baseDir(new File("appRoot")) .build(new HandlerFactory() { public Handler create(LaunchConfig launchConfig) { return Guice.handler(launchConfig, new Bindings(), new ChainAction() { protected void execute() { handler(chain.getRegistry().get(MyHandler.class)); } }); } });Example usage: (Groovy DSL)
import ratpack.groovy.markuptemplates.MarkupTemplatingModule import static ratpack.groovy.Groovy.groovyMarkupTemplate import static ratpack.groovy.Groovy.ratpack ratpack { bindings { add new MarkupTemplatingModule() } handlers { get { render groovyMarkupTemplate('my/template/path.gtpl', key: 'it works!') } } }
Constructor and Description |
---|
MarkupTemplatingModule() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure() |
String |
getTemplatesDirectory() |
void |
setTemplatesDirectory(String templatesDirectory) |
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 getTemplatesDirectory()
public void setTemplatesDirectory(String templatesDirectory)
protected void configure()
configure
in class AbstractModule