public class Handlebars extends java.lang.Object implements HelperRegistry
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.
Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline("Hello {{this}}!"); System.out.println(template.apply("Handlebars.java"));
This example load mytemplate.hbs
from the root of the classpath:
Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline(URI.create("mytemplate")); System.out.println(template.apply("Handlebars.java"));
You can specify a different ```TemplateLoader``` by:
TemplateLoader loader = ...; Handlebars handlebars = new Handlebars(loader);
Modifier and Type | Class and Description |
---|---|
static class |
Handlebars.SafeString
A
Handlebars.SafeString tell Handlebars that the content should not be
escaped as HTML. |
static class |
Handlebars.Utils
Utilities function like:
Handlebars.Utils.escapeExpression(CharSequence) and
Handlebars.Utils.isEmpty(Object) . |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DELIM_END
The default end delimiter.
|
static java.lang.String |
DELIM_START
The default start delimiter.
|
HELPER_MISSING
Constructor and Description |
---|
Handlebars()
Creates a new
Handlebars with a ClassPathTemplateLoader and no
cache. |
Handlebars(TemplateLoader loader)
Creates a new
Handlebars with no cache. |
Modifier and Type | Method and Description |
---|---|
Template |
compile(java.lang.String location)
Compile the resource located at the given uri.
|
Template |
compile(java.lang.String location,
java.lang.String startDelimiter,
java.lang.String endDelimiter)
Compile the resource located at the given uri.
|
Template |
compile(TemplateSource source)
Compile a handlebars template.
|
Template |
compile(TemplateSource source,
java.lang.String startDelimiter,
java.lang.String endDelimiter)
Compile a handlebars template.
|
Template |
compileInline(java.lang.String input)
Compile a handlebars template.
|
Template |
compileInline(java.lang.String input,
java.lang.String startDelimiter,
java.lang.String endDelimiter)
Compile a handlebars template.
|
static void |
debug(java.lang.String message)
Log the given message as debug and format the message within the args.
|
static void |
debug(java.lang.String message,
java.lang.Object... args)
Log the given message as debug and format the message within the args.
|
Decorator |
decorator(java.lang.String name)
Find a decorator by name.
|
boolean |
deletePartialAfterMerge()
If true, templates will be deleted once applied.
|
Handlebars |
deletePartialAfterMerge(boolean deletePartialAfterMerge)
If true, templates will be deleted once applied.
|
Handlebars |
endDelimiter(java.lang.String endDelimiter)
Set the end delimiter.
|
static void |
error(java.lang.String message)
Log the given message as error and format the message within the args.
|
static void |
error(java.lang.String message,
java.lang.Object... args)
Log the given message as error and format the message within the args.
|
TemplateCache |
getCache()
The template cache.
|
EscapingStrategy |
getEscapingStrategy()
The escaping strategy.
|
Formatter.Chain |
getFormatter() |
TemplateLoader |
getLoader()
The resource locator.
|
ParserFactory |
getParserFactory()
Return a parser factory.
|
java.lang.String |
handlebarsJsFile() |
Handlebars |
handlebarsJsFile(java.lang.String location)
Set the handlebars.js location used it to compile/precompile template to JavaScript.
|
<C> Helper<C> |
helper(java.lang.String name)
Find a helper by name.
|
java.util.Set<java.util.Map.Entry<java.lang.String,Helper<?>>> |
helpers()
List all the helpers from registry.
|
boolean |
infiniteLoops()
If true, templates will be able to call him self directly or indirectly.
|
Handlebars |
infiniteLoops(boolean infiniteLoops)
If true, templates will be able to call him self directly or indirectly.
|
static void |
log(java.lang.String message)
Log the given message and format the message within the args.
|
static void |
log(java.lang.String message,
java.lang.Object... args)
Log the given message and format the message within the args.
|
boolean |
parentScopeResolution() |
Handlebars |
parentScopeResolution(boolean parentScopeResolution)
Given:
|
boolean |
prettyPrint()
If true, unnecessary spaces and new lines will be removed from output.
|
Handlebars |
prettyPrint(boolean prettyPrint)
If true, unnecessary spaces and new lines will be removed from output.
|
Handlebars |
registerDecorator(java.lang.String name,
Decorator decorator)
Register a decorator and make it accessible via
HelperRegistry.decorator(String) . |
<H> Handlebars |
registerHelper(java.lang.String name,
Helper<H> helper)
Register a helper in the helper registry.
|
<H> Handlebars |
registerHelperMissing(Helper<H> helper)
Register a missing helper in the helper registry.
|
Handlebars |
registerHelpers(java.lang.Class<?> helperSource)
Register all the helper methods for the given helper source.
|
Handlebars |
registerHelpers(java.io.File input)
Register helpers from a JavaScript source.
|
Handlebars |
registerHelpers(java.lang.Object helperSource)
Register all the helper methods for the given helper source.
|
Handlebars |
registerHelpers(java.lang.String filename,
java.io.InputStream source)
Register helpers from a JavaScript source.
|
Handlebars |
registerHelpers(java.lang.String filename,
java.io.Reader source)
Register helpers from a JavaScript source.
|
Handlebars |
registerHelpers(java.lang.String filename,
java.lang.String source)
Register helpers from a JavaScript source.
|
Handlebars |
registerHelpers(java.net.URI location)
Register helpers from a JavaScript source.
|
void |
setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
If true, templates will be deleted once applied.
|
void |
setEndDelimiter(java.lang.String endDelimiter)
Set the end delimiter.
|
void |
setInfiniteLoops(boolean infiniteLoops)
If true, templates will be able to call him self directly or indirectly.
|
void |
setParentScopeResolution(boolean parentScopeResolution)
Given:
|
void |
setPrettyPrint(boolean prettyPrint)
If true, unnecessary spaces and new lines will be removed from output.
|
void |
setStartDelimiter(java.lang.String startDelimiter)
Set the start delimiter.
|
void |
setStringParams(boolean stringParams)
If true, missing helper parameters will be resolve to their names.
|
Handlebars |
startDelimiter(java.lang.String startDelimiter)
Set the start delimiter.
|
boolean |
stringParams()
If true, missing helper parameters will be resolve to their names.
|
Handlebars |
stringParams(boolean stringParams)
If true, missing helper parameters will be resolve to their names.
|
static void |
warn(java.lang.String message)
Log the given message as warn and format the message within the args.
|
static void |
warn(java.lang.String message,
java.lang.Object... args)
Log the given message as warn and format the message within the args.
|
Handlebars |
with(EscapingStrategy escapingStrategy)
Set a new
EscapingStrategy . |
Handlebars |
with(Formatter formatter)
Add a new variable formatter.
|
Handlebars |
with(HelperRegistry registry)
Set the helper registry.
|
Handlebars |
with(ParserFactory parserFactory)
Set a new
ParserFactory . |
Handlebars |
with(TemplateCache cache)
Set a new
TemplateCache . |
Handlebars |
with(TemplateLoader... loader)
Set one or more
TemplateLoader . |
public static final java.lang.String DELIM_START
public static final java.lang.String DELIM_END
public Handlebars(TemplateLoader loader)
Handlebars
with no cache.loader
- The template loader. Required.public Handlebars()
Handlebars
with a ClassPathTemplateLoader
and no
cache.public Template compile(java.lang.String location) throws java.io.IOException
location
- The resource's location. Required.java.io.IOException
- If the resource cannot be loaded.public Template compile(java.lang.String location, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
location
- The resource's location. Required.startDelimiter
- The start delimiter. Required.endDelimiter
- The end delimiter. Required.java.io.IOException
- If the resource cannot be loaded.public Template compileInline(java.lang.String input) throws java.io.IOException
input
- The handlebars input. Required.java.io.IOException
- If the resource cannot be loaded.public Template compileInline(java.lang.String input, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
input
- The input text. Required.startDelimiter
- The start delimiter. Required.endDelimiter
- The end delimiter. Required.java.io.IOException
- If the resource cannot be loaded.public Template compile(TemplateSource source) throws java.io.IOException
source
- The template source. Required.java.io.IOException
- If the resource cannot be loaded.public Template compile(TemplateSource source, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
source
- The template source. Required.startDelimiter
- The start delimiter. Required.endDelimiter
- The end delimiter. Required.java.io.IOException
- If the resource cannot be loaded.public <C> Helper<C> helper(java.lang.String name)
helper
in interface HelperRegistry
C
- The helper runtime type.name
- The helper's name. Required.public <H> Handlebars registerHelper(java.lang.String name, Helper<H> helper)
registerHelper
in interface HelperRegistry
H
- The helper runtime type.name
- The helper's name. Required.helper
- The helper object. Required.public <H> Handlebars registerHelperMissing(Helper<H> helper)
registerHelperMissing
in interface HelperRegistry
H
- The helper runtime type.helper
- The helper object. Required.public Handlebars registerHelpers(java.lang.Object helperSource)
Register all the helper methods for the given helper source.
A helper method looks like:
public static? CharSequence methodName(context?, parameter*, options?) { }Where:
registerHelpers
in interface HelperRegistry
helperSource
- The helper source. Required.public Handlebars registerHelpers(java.lang.Class<?> helperSource)
Register all the helper methods for the given helper source.
A helper method looks like:
public static? CharSequence methodName(context?, parameter*, options?) { }Where:
Enums are supported too
registerHelpers
in interface HelperRegistry
helperSource
- The helper source. Enums are supported. Required.public Handlebars registerHelpers(java.net.URI location) throws java.lang.Exception
Register helpers from a JavaScript source.
A JavaScript source file looks like:
Handlebars.registerHelper('hey', function (context) { return 'Hi ' + context.name; }); ... Handlebars.registerHelper('hey', function (context, options) { return 'Hi ' + context.name + options.hash['x']; }); ... Handlebars.registerHelper('hey', function (context, p1, p2, options) { return 'Hi ' + context.name + p1 + p2 + options.hash['x']; }); ...To keep your helpers reusable between server and client avoid DOM manipulation.
registerHelpers
in interface HelperRegistry
location
- A classpath location. Required.java.lang.Exception
- If the JavaScript helpers can't be registered.public Handlebars registerHelpers(java.io.File input) throws java.lang.Exception
Register helpers from a JavaScript source.
A JavaScript source file looks like:
Handlebars.registerHelper('hey', function (context) { return 'Hi ' + context.name; }); ... Handlebars.registerHelper('hey', function (context, options) { return 'Hi ' + context.name + options.hash['x']; }); ... Handlebars.registerHelper('hey', function (context, p1, p2, options) { return 'Hi ' + context.name + p1 + p2 + options.hash['x']; }); ...To keep your helpers reusable between server and client avoid DOM manipulation.
registerHelpers
in interface HelperRegistry
input
- A JavaScript file name. Required.java.lang.Exception
- If the JavaScript helpers can't be registered.public Handlebars registerHelpers(java.lang.String filename, java.io.Reader source) throws java.lang.Exception
Register helpers from a JavaScript source.
A JavaScript source file looks like:
Handlebars.registerHelper('hey', function (context) { return 'Hi ' + context.name; }); ... Handlebars.registerHelper('hey', function (context, options) { return 'Hi ' + context.name + options.hash['x']; }); ... Handlebars.registerHelper('hey', function (context, p1, p2, options) { return 'Hi ' + context.name + p1 + p2 + options.hash['x']; }); ...To keep your helpers reusable between server and client avoid DOM manipulation.
registerHelpers
in interface HelperRegistry
filename
- The file name (just for debugging purpose). Required.source
- The JavaScript source. Required.java.lang.Exception
- If the JavaScript helpers can't be registered.public Handlebars registerHelpers(java.lang.String filename, java.io.InputStream source) throws java.lang.Exception
Register helpers from a JavaScript source.
A JavaScript source file looks like:
Handlebars.registerHelper('hey', function (context) { return 'Hi ' + context.name; }); ... Handlebars.registerHelper('hey', function (context, options) { return 'Hi ' + context.name + options.hash['x']; }); ... Handlebars.registerHelper('hey', function (context, p1, p2, options) { return 'Hi ' + context.name + p1 + p2 + options.hash['x']; }); ...To keep your helpers reusable between server and client avoid DOM manipulation.
registerHelpers
in interface HelperRegistry
filename
- The file name (just for debugging purpose). Required.source
- The JavaScript source. Required.java.lang.Exception
- If the JavaScript helpers can't be registered.public Handlebars registerHelpers(java.lang.String filename, java.lang.String source) throws java.lang.Exception
Register helpers from a JavaScript source.
A JavaScript source file looks like:
Handlebars.registerHelper('hey', function (context) { return 'Hi ' + context.name; }); ... Handlebars.registerHelper('hey', function (context, options) { return 'Hi ' + context.name + options.hash['x']; }); ... Handlebars.registerHelper('hey', function (context, p1, p2, options) { return 'Hi ' + context.name + p1 + p2 + options.hash['x']; }); ...To keep your helpers reusable between server and client avoid DOM manipulation.
registerHelpers
in interface HelperRegistry
filename
- The file name (just for debugging purpose). Required.source
- The JavaScript source. Required.java.lang.Exception
- If the JavaScript helpers can't be registered.public java.util.Set<java.util.Map.Entry<java.lang.String,Helper<?>>> helpers()
HelperRegistry
helpers
in interface HelperRegistry
public TemplateLoader getLoader()
public TemplateCache getCache()
public EscapingStrategy getEscapingStrategy()
public boolean stringParams()
public boolean prettyPrint()
public void setPrettyPrint(boolean prettyPrint)
prettyPrint
- If true, unnecessary spaces and new lines will be removed from output.
Default is: false.public Handlebars prettyPrint(boolean prettyPrint)
prettyPrint
- If true, unnecessary spaces and new lines will be removed from output.
Default is: false.public void setStringParams(boolean stringParams)
stringParams
- If true, missing helper parameters will be resolve to
their names.public Handlebars stringParams(boolean stringParams)
stringParams
- If true, missing helper parameters will be resolve to
their names.public boolean infiniteLoops()
public void setInfiniteLoops(boolean infiniteLoops)
infiniteLoops
- If true, templates will be able to call him self directly or
indirectly.public Handlebars infiniteLoops(boolean infiniteLoops)
infiniteLoops
- If true, templates will be able to call him self directly or
indirectly.public boolean deletePartialAfterMerge()
{{#block}} helper
. Default is: false.
At any time you can override the default setup with:
{{#block "footer" delete-after-merge=true}}
{{#block}} helper
.public Handlebars deletePartialAfterMerge(boolean deletePartialAfterMerge)
{{#block}} helper
. Default is: false.
At any time you can override the default setup with:
{{#block "footer" delete-after-merge=true}}
deletePartialAfterMerge
- True for clearing up templates once they got applied. Used by
{{#block}} helper
.public void setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
{{#block}} helper
. Default is: false.
At any time you can override the default setup with:
{{#block "footer" delete-after-merge=true}}
deletePartialAfterMerge
- True for clearing up templates once they got applied. Used by
{{#block}} helper
.public void setEndDelimiter(java.lang.String endDelimiter)
endDelimiter
- The end delimiter. Required.public Handlebars endDelimiter(java.lang.String endDelimiter)
endDelimiter
- The end delimiter. Required.public void setStartDelimiter(java.lang.String startDelimiter)
startDelimiter
- The start delimiter. Required.public Handlebars startDelimiter(java.lang.String startDelimiter)
startDelimiter
- The start delimiter. Required.public Handlebars with(TemplateLoader... loader)
TemplateLoader
. In the case of two or more TemplateLoader
, a
CompositeTemplateLoader
will be created. Default is: ClassPathTemplateLoader
.loader
- The template loader. Required.CompositeTemplateLoader
public Handlebars with(ParserFactory parserFactory)
ParserFactory
.parserFactory
- A parser factory. Required.public Handlebars with(TemplateCache cache)
TemplateCache
.cache
- The template cache. Required.public Handlebars with(HelperRegistry registry)
registry
- The helper registry. Required.public Handlebars with(EscapingStrategy escapingStrategy)
EscapingStrategy
.escapingStrategy
- The escaping strategy. Required.public Formatter.Chain getFormatter()
public Handlebars with(Formatter formatter)
Handlebars hbs = new Handlebars(); hbs.with(new Formatter() { public Object format(Object value, Chain next) { if (value instanceof Date) { return ((Date) value).getTime(); } return next.format(value); } });
formatter
- A formatter.public Handlebars handlebarsJsFile(java.lang.String location)
Using handlebars.js 4.x:
Handlebars handlebars = new Handlebars() .handlebarsJsFile("handlebars-v4.0.4.js");
Using handlebars.js 1.x:
Handlebars handlebars = new Handlebars() .handlebarsJsFile("handlebars-v1.3.0.js");Default handlebars.js is
handlebars-v4.0.4.js
.location
- A classpath location of the handlebar.js file.public java.lang.String handlebarsJsFile()
handlebars-v4.0.4.js
public boolean parentScopeResolution()
public void setParentScopeResolution(boolean parentScopeResolution)
{ "value": "Brett", "child": { "bestQB" : "Favre" } }Handlebars.java will output:
Hello Favre Brett
while handlebars.js:
Hello Favre
.
Why? Handlebars.java is a 100% Mustache implementation while handlebars.js isn't.
This option forces Handlebars.java mimics handlebars.js behavior:
Handlebars hbs = new Handlebars() .parentScopeResolution(true);Outputs:
Hello Favre
.parentScopeResolution
- False, if we want to restrict lookup to current scope (like in
handlebars.js). Default is true
public Handlebars parentScopeResolution(boolean parentScopeResolution)
{ "value": "Brett", "child": { "bestQB" : "Favre" } }Handlebars.java will output:
Hello Favre Brett
while handlebars.js:
Hello Favre
.
Why? Handlebars.java is a 100% Mustache implementation while handlebars.js isn't.
This option forces Handlebars.java mimics handlebars.js behavior:
Handlebars hbs = new Handlebars() .parentScopeResolution(true);Outputs:
Hello Favre
.parentScopeResolution
- False, if we want to restrict lookup to current scope (like in
handlebars.js). Default is true
public ParserFactory getParserFactory()
public static void log(java.lang.String message, java.lang.Object... args)
message
- The log's message.args
- The optional args.String.format(String, Object...)
public static void log(java.lang.String message)
message
- The log's message.String.format(String, Object...)
public static void warn(java.lang.String message, java.lang.Object... args)
message
- The log's message.args
- The optional args.String.format(String, Object...)
public static void warn(java.lang.String message)
message
- The log's message.String.format(String, Object...)
public static void debug(java.lang.String message, java.lang.Object... args)
message
- The log's message.args
- The optional args.String.format(String, Object...)
public static void debug(java.lang.String message)
message
- The log's message.String.format(String, Object...)
public static void error(java.lang.String message, java.lang.Object... args)
message
- The log's message.args
- The optional args.String.format(String, Object...)
public static void error(java.lang.String message)
message
- The log's message.String.format(String, Object...)
public Decorator decorator(java.lang.String name)
HelperRegistry
decorator
in interface HelperRegistry
name
- A decorator's name.null
.public Handlebars registerDecorator(java.lang.String name, Decorator decorator)
HelperRegistry
HelperRegistry.decorator(String)
.registerDecorator
in interface HelperRegistry
name
- A decorator's name. Required.decorator
- A decorator. Required.Copyright © 2010 - 2020 Adobe. All Rights Reserved