Mustache/Handlebars templates for Jooby.
Handlebars- ViewEngine
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-hbs</artifactId>
<version>1.0.0.CR6</version>
</dependency>It is pretty straightforward:
{
use(new Hbs());
get("/", req -> Results.html("index").put("model", new MyModel());
}public/index.html:
{{ "{{ model " }}}}
Templates are loaded from root of classpath: / and must end with: .html file extension.
A template engine has access to request locals (a.k.a attributes). Here is an example:
{
use(new Hbs());
get("*", req -> {
req.set("req", req);
req.set("session", req.session());
});
}By default, there is no access to req or session from your template. This example shows how to do it.
Simple/basic helpers are add it at startup time:
{
use(new Hbs().doWith((hbs, config) -> {
hbs.registerHelper("myhelper", (ctx, options) -> {
return ...;
});
hbs.registerHelpers(Helpers.class);
});
}Now, if the helper depends on a service and require injection:
{
use(new Hbs().with(Helpers.class));
}The Helpers will be injected by Guice and Handlebars will scan and discover any helper method.
Templates are loaded from the root of classpath and must end with .html. You can
change the default template location and extensions too:
{
use(new Hbs("/", ".hbs"));
}Cache is OFF when env=dev (useful for template reloading), otherwise is ON.
Cache is backed by Guava and the default cache will expire after 100 entries.
If 100 entries is not enough or you need a more advanced cache setting, just set the
hbs.cache option:
hbs.cache = "expireAfterWrite=1h"See CacheBuilderSpec for more detailed expressions.
That's all folks! Enjoy it!!!