jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.44k stars 382 forks source link

render partial in custom helper #797

Open ngrinberg opened 3 years ago

ngrinberg commented 3 years ago

is it possible to do what you can do in the js https://github.com/handlebars-lang/handlebars.js/issues/1057 where you are able to compile a partial in a custom helper?

esmin commented 3 years ago

Yes, It is possible.

public static Object partialHelper(String partialPath, Options options) throws IOException {
        Template t = //get Template via loader using partialPath
        return new Handlebars.SafeString(t.apply(options.context.combine(options.hash)));
}

SafeString will prevent partial from being escaped twice context.combine(options.hash) will pass hash params for use in partial

{{partialHelper '_header' title='my page'.... }}