Basic Usage: hash + template -> render
Variable:
Unescaped: {{{name}}} or {{& name }}
Section: {{#boolean_val}} {{/boolean_val}}
Inverted Sections {{^key}} {{/key}}
Comments {{! some comment }}
Partials {{> key }}
Template inheritance
{{> (partialNameHelper) }}
Set delimiter {{=<% %>=}} change {{ }} to <% %>
http://jknack.github.io/handlebars.java/
Handlebar has helpers to extend mustache, some built-ins are:
with {{#with story}} ... {{/with}} - switch context
each {{#each a_collection }} ... {{/each}} / can do with plain mustache {{#
if {{#if active}} ...{{else}} ... {{/if}} / or plain mustache
unless {{#unless active}}...{{/unless}} / can do with plain mustache
Helpers:
See http://jknack.github.io/handlebars.java/helpers.html
Basic helpers {{name context? [argument]* [hash]*}}
Block helpers {{#name context? [argument]* [hash]* }} ... {{/name}}
name context? [argument]* [hash]*}}
...
{{/name}}
Name, context, argument, and hash are defined as follows:
name: qid;
context: expression;
argument: expression;
hash: id '=' expression;
expression: literal | qid;
qid
:
'../' qid
| id '[' .* ']' ('.' | '/') qid
| id '[' .* ']' qid
| id ('.' | '/') qid
| 'id'
;
id: idStart (idStart | idPart)*;
idStart: [a-zA-Z_$@];
idPart: [0-9];
literal
:
stringLiteral
| boolLiteral
| numberLiteral
| 'this'
| '.'
;
stringLiteral:'"' .* '"';
boolLiteral:'true' | 'false';
numberLiteral: '0'..'9'+;
To register custom helpers
Register one at a time:
handlebars.registerHelper("blog", new Helper<Blog>() {
public CharSequence apply(Blog blog, Options options) {
return options.fn(blog);
}
});
To register multiple helpers at once, register a
Helper Source
Example:
public class HelperSource {
public String blog(Blog blog, Options options) {
return options.fn(blog);
}
public static String now() {
return new Date().toString();
}
public String render(Blog context, String param0, int param1,
boolean param2, Options options) {
return ...
}
}
Register: an instance, or xx.class
Helper options:
Parameter with position, with method argument, with auto boxing:
{{blog this "arg0"}}
...
public CharSequence apply(Blog blog, Options options) {
return options.param(0);
}
{{blog this "arg0"}}
...
public CharSequence blog(Blog blog, String arg0) {
return arg0;
}
{{blog this "string" true 678}}
...
public CharSequence blog(Blog blog, Options options) {
String str = options.param(0);
boolean bool = options.param(1);
int num = options.param(2);
}
{{blog this "string" true 678}}
...
public CharSequence blog(Blog blog, String str, boolean bool, int num) {
}
Access a context stack value
{{blog this author}}
...
public CharSequence blog(Blog blog, Author author) {
return author.getName();
}
handlebars.registerHelper(Handlebars.HELPER_MISSING
Registers a default helper
http://jknack.github.io/handlebars.java/loader.html
Able to load templates from file system, class path, web context and more
APIs: (no need to implement unless using non-default source)
ClassPathTemplateLoader
FileTemplateLoader: see doc
ServletContextTemplateLoader: see doc
CompositeTemplateLoader: combine two or more loaders
Important concept. com.github.jknack.handlebars.Context object represents the context stack.
Resolution algorithm looks like: (first try current context, if not found, try parent, until resolved or resolved to null and nothing rendered)
while (context != null && context.get("value") == null) {
context = context.parent;
}
Extending Context Stack
See doc