Implementations of this interface bound with Guice will be automatically registered as handlebars helpers.
import com.github.jknack.handlebars.Options;
import ratpack.guice.Guice;
import ratpack.handlebars.HandlebarsModule;
import ratpack.handlebars.NamedHelper;
import ratpack.test.embed.BaseDirBuilder;
import ratpack.test.embed.EmbeddedApp;
import java.io.IOException;
import java.nio.file.Path;
import static ratpack.handlebars.Template.handlebarsTemplate;
public class Example {
public static class HelloHelper implements NamedHelper<String> {
public String getName() {
return "hello";
}
public CharSequence apply(String context, Options options) throws IOException {
return "Hello " + context.toUpperCase() + "!";
}
}
public static void main(String... args) {
Path baseDir = BaseDirBuilder.tmpDir().build(builder ->
builder.file("handlebars/myTemplate.html.hbs", "{{hello \"ratpack\"}}")
);
EmbeddedApp.fromHandlerFactory(baseDir, launchConfig ->
Guice.builder(launchConfig)
.bindings(b -> b
.add(new HandlebarsModule())
.bind(HelloHelper.class)
)
.build(chain -> chain
.get(ctx -> ctx.render(handlebarsTemplate("myTemplate.html")))
)
).test(httpClient -> {
assert httpClient.getText().equals("Hello RATPACK!");
});
}
}