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.EphemeralBaseDir;
import ratpack.test.embed.EmbeddedApp;
import java.io.IOException;
import java.nio.file.Path;
import static ratpack.handlebars.Template.handlebarsTemplate;
import static org.junit.Assert.*;
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) throws Exception {
EphemeralBaseDir.tmpDir().use(baseDir -> {
baseDir.write("handlebars/myTemplate.html.hbs", "{{hello \"ratpack\"}}");
EmbeddedApp.of(s -> s
.serverConfig(c -> c.baseDir(baseDir.getRoot()))
.registry(Guice.registry(b -> b
.module(new HandlebarsModule())
.bind(HelloHelper.class)
))
.handlers(chain -> chain
.get(ctx -> ctx.render(handlebarsTemplate("myTemplate.html")))
)
).test(httpClient -> {
assertEquals("Hello RATPACK!", httpClient.getText());
});
});
}
}