public class HandlebarsModule 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.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.
So handlebarsTemplate("my/template/path") maps to handlebars/my/template/path.hbs in the application root directory.
This can be configured using setTemplatesPath(String) and setTemplatesSuffix(String) as well as
other.handlebars.templatesPath and other.handlebars.templatesSuffix configuration properties.
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 using
MimeTypes contextual object so content type for handlebarsTemplate("template.html")
would be text/html by default.
Custom handlebars helpers can be registered by binding instances of NamedHelper.
Example usage: (Java DSL)
import ratpack.handling.*;
import ratpack.guice.*;
import ratpack.func.Action;
import ratpack.launch.*;
import ratpack.handlebars.HandlebarsModule;
import static ratpack.handlebars.Template.handlebarsTemplate;
class MyHandler implements Handler {
void handle(final Context context) {
context.render(handlebarsTemplate("my/template/path", key: "it works!"));
}
}
class ModuleBootstrap implements Action<BindingsSpec> {
public void execute(BindingsSpec bindings) {
bindings.add(new HandlebarsModule());
}
}
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.handlebars.HandlebarsModule
import static ratpack.handlebars.Template.handlebarsTemplate
import static ratpack.groovy.Groovy.ratpack
ratpack {
bindings {
add new HandlebarsModule()
}
handlers {
get {
render handlebarsTemplate('my/template/path', key: 'it works!')
}
}
}
| Constructor and Description |
|---|
HandlebarsModule() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
configure() |
int |
getCacheSize() |
String |
getTemplatesPath() |
String |
getTemplatesSuffix() |
boolean |
isReloadable() |
void |
setCacheSize(int cacheSize) |
void |
setReloadable(boolean reloadable) |
void |
setTemplatesPath(String templatesPath) |
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, requireBindingpublic String getTemplatesPath()
public void setTemplatesPath(String templatesPath)
public String getTemplatesSuffix()
public void setTemplatesSuffix(String templatesSuffix)
public int getCacheSize()
public void setCacheSize(int cacheSize)
public boolean isReloadable()
public void setReloadable(boolean reloadable)
protected void configure()
configure in class AbstractModule