public class Hbs extends Object implements Jooby.Module
Logic-less and semantic Mustache templates via handlebars.java.
Handlebars object.Renderer object.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.
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.
That's all folks! Enjoy it!!!
| Constructor and Description |
|---|
Hbs(Class<?>... helpers)
Creates a new
Hbs module. |
Hbs(String prefix,
Class<?>... helpers)
Creates a new
Hbs module. |
Hbs(String prefix,
String suffix,
Class<?>... helpers)
Creates a new
Hbs module. |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Hbs |
doWith(BiConsumer<com.github.jknack.handlebars.Handlebars,com.typesafe.config.Config> callback)
Set a handlebars callback.
|
Hbs |
doWith(Consumer<com.github.jknack.handlebars.Handlebars> callback)
Set a handlebars callback.
|
Hbs |
with(Class<?>... helper)
Append one or more helper classes.
|
Hbs |
with(com.github.jknack.handlebars.ValueResolver resolver)
Append a
ValueResolver. |
public Hbs(String prefix, String suffix, Class<?>... helpers)
Hbs module.prefix - Template prefix.suffix - Template suffix.helpers - Optional list of helpers.public Hbs(String prefix, Class<?>... helpers)
Hbs module.prefix - Template prefix.helpers - Optional list of helpers.public Hbs doWith(BiConsumer<com.github.jknack.handlebars.Handlebars,com.typesafe.config.Config> callback)
{
use(new Hbs().doWith((hbs, conf) -> {
...
});
}
callback - Configurer callback.public Hbs doWith(Consumer<com.github.jknack.handlebars.Handlebars> callback)
{
use(new Hbs().doWith((hbs, conf) -> {
...
});
}
callback - Configurer callback.public Hbs with(Class<?>... helper)
helper - Helper class.public Hbs with(com.github.jknack.handlebars.ValueResolver resolver)
ValueResolver.resolver - Resolver.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2019. All rights reserved.