Class HandlebarsModule
- java.lang.Object
-
- com.google.inject.AbstractModule
-
- ratpack.guice.ConfigurableModule<HandlebarsModule.Config>
-
- ratpack.handlebars.HandlebarsModule
-
- All Implemented Interfaces:
com.google.inject.Module
public class HandlebarsModule extends ConfigurableModule<HandlebarsModule.Config>
An extension module that provides support for Handlebars.java templating engine.To use it one has to register the module and then render
Template
instances. Instances ofTemplate
can be created using one of theTemplate.handlebarsTemplate(java.util.Map, String, String)
static methods.By default templates are looked up in the
handlebars
directory of the application root with a.hbs
suffix. SohandlebarsTemplate("my/template/path")
maps tohandlebars/my/template/path.hbs
in the application root directory. This can be configured usingHandlebarsModule.Config.templatesPath(String)
andHandlebarsModule.Config.templatesSuffix(String)
.The default template delimiters are
{{ }}
but can be configured usingHandlebarsModule.Config.delimiters(String, String)
.Response content type can be manually specified, i.e.
handlebarsTemplate("template", model, "text/html")
or can be detected based on the template extension. Mapping between file extensions and content types is performed usingMimeTypes
contextual object so content type forhandlebarsTemplate("template.html")
would betext/html
by default.Custom handlebars helpers can be registered by binding instances of
NamedHelper
.import ratpack.guice.Guice; import ratpack.handlebars.HandlebarsModule; import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import java.nio.file.Path; import static ratpack.handlebars.Template.handlebarsTemplate; import static org.junit.jupiter.api.Assertions.*; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("handlebars/myTemplate.html.hbs", "Hello {{name}}!"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .registry(Guice.registry(b -> b.module(HandlebarsModule.class))) .handlers(chain -> chain .get(ctx -> ctx.render(handlebarsTemplate("myTemplate.html", m -> m.put("name", "Ratpack")))) ) ).test(httpClient -> { assertEquals("Hello Ratpack!", httpClient.getText()); }); }); } }
- See Also:
- Handlebars.java
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HandlebarsModule.Config
The configuration object forHandlebarsModule
.
-
Constructor Summary
Constructors Constructor Description HandlebarsModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
configure()
-
Methods inherited from class ratpack.guice.ConfigurableModule
configure, createConfig, defaultConfig, 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
-
-