Class Handlebars
- java.lang.Object
-
- com.github.jknack.handlebars.Handlebars
-
- All Implemented Interfaces:
HelperRegistry
public class Handlebars extends java.lang.Object implements HelperRegistry
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.
Getting Started:
Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline("Hello {{this}}!"); System.out.println(template.apply("Handlebars.java"));Loading templates
Templates are loaded using the ```TemplateLoader``` class. Handlebars.java provides three implementations of a ```TemplateLoader```:- ClassPathTemplateLoader (default)
- FileTemplateLoader
- SpringTemplateLoader (available at the handlebars-springmvc module)
This example load
mytemplate.hbsfrom 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);
- Since:
- 0.1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHandlebars.SafeStringAHandlebars.SafeStringtellHandlebarsthat the content should not be escaped as HTML.static classHandlebars.UtilsUtilities function like:Handlebars.Utils.escapeExpression(CharSequence)andHandlebars.Utils.isEmpty(Object).
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDELIM_ENDThe default end delimiter.static java.lang.StringDELIM_STARTThe default start delimiter.-
Fields inherited from interface com.github.jknack.handlebars.HelperRegistry
HELPER_MISSING
-
-
Constructor Summary
Constructors Constructor Description Handlebars()Creates a newHandlebarswith aClassPathTemplateLoaderand no cache.Handlebars(TemplateLoader loader)Creates a newHandlebarswith no cache.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Templatecompile(TemplateSource source)Compile a handlebars template.Templatecompile(TemplateSource source, java.lang.String startDelimiter, java.lang.String endDelimiter)Compile a handlebars template.Templatecompile(java.lang.String location)Compile the resource located at the given uri.Templatecompile(java.lang.String location, java.lang.String startDelimiter, java.lang.String endDelimiter)Compile the resource located at the given uri.TemplatecompileInline(java.lang.String input)Compile a handlebars template.TemplatecompileInline(java.lang.String input, java.lang.String startDelimiter, java.lang.String endDelimiter)Compile a handlebars template.static voiddebug(java.lang.String message)Log the given message as debug and format the message within the args.static voiddebug(java.lang.String message, java.lang.Object... args)Log the given message as debug and format the message within the args.Decoratordecorator(java.lang.String name)Find a decorator by name.booleandeletePartialAfterMerge()If true, templates will be deleted once applied.HandlebarsdeletePartialAfterMerge(boolean deletePartialAfterMerge)If true, templates will be deleted once applied.HandlebarsendDelimiter(java.lang.String endDelimiter)Set the end delimiter.static voiderror(java.lang.String message)Log the given message as error and format the message within the args.static voiderror(java.lang.String message, java.lang.Object... args)Log the given message as error and format the message within the args.TemplateCachegetCache()The template cache.java.nio.charset.CharsetgetCharset()java.lang.StringgetEndDelimiter()The End Delimiter.EscapingStrategygetEscapingStrategy()The escaping strategy.Formatter.ChaingetFormatter()TemplateLoadergetLoader()The resource locator.ParserFactorygetParserFactory()Return a parser factory.java.lang.StringgetStartDelimiter()The Start Delimiter.java.lang.StringhandlebarsJsFile()HandlebarshandlebarsJsFile(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.booleaninfiniteLoops()If true, templates will be able to call him self directly or indirectly.HandlebarsinfiniteLoops(boolean infiniteLoops)If true, templates will be able to call him self directly or indirectly.static voidlog(java.lang.String message)Log the given message and format the message within the args.static voidlog(java.lang.String message, java.lang.Object... args)Log the given message and format the message within the args.booleanparentScopeResolution()HandlebarsparentScopeResolution(boolean parentScopeResolution)Given:java.lang.Stringprecompile(java.lang.String path)Precompile a template to JavaScript.java.lang.StringprecompileInline(java.lang.String template)Precompile a template to JavaScript.booleanpreEvaluatePartialBlocks()If true, partial blocks will implicitly be evaluated before the partials will actually be executed.HandlebarspreEvaluatePartialBlocks(boolean preEvaluatePartialBlocks)If true, partial blocks will implicitly be evaluated before the partials will actually be executed.booleanprettyPrint()If true, unnecessary spaces and new lines will be removed from output.HandlebarsprettyPrint(boolean prettyPrint)If true, unnecessary spaces and new lines will be removed from output.HandlebarsregisterDecorator(java.lang.String name, Decorator decorator)Register a decorator and make it accessible viaHelperRegistry.decorator(String).<H> HandlebarsregisterHelper(java.lang.String name, Helper<H> helper)Register a helper in the helper registry.<H> HandlebarsregisterHelperMissing(Helper<H> helper)Register a missing helper in the helper registry.HandlebarsregisterHelpers(java.io.File input)Register helpers from a JavaScript source.HandlebarsregisterHelpers(java.lang.Class<?> helperSource)Register all the helper methods for the given helper source.HandlebarsregisterHelpers(java.lang.Object helperSource)Register all the helper methods for the given helper source.HandlebarsregisterHelpers(java.lang.String filename, java.io.InputStream source)Register helpers from a JavaScript source.HandlebarsregisterHelpers(java.lang.String filename, java.io.Reader source)Register helpers from a JavaScript source.HandlebarsregisterHelpers(java.lang.String filename, java.lang.String source)Register helpers from a JavaScript source.HandlebarsregisterHelpers(java.net.URI location)Register helpers from a JavaScript source.HandlebarssetCharset(java.nio.charset.Charset charset)Set the charset to use.voidsetDeletePartialAfterMerge(boolean deletePartialAfterMerge)If true, templates will be deleted once applied.voidsetEndDelimiter(java.lang.String endDelimiter)Set the end delimiter.voidsetInfiniteLoops(boolean infiniteLoops)If true, templates will be able to call him self directly or indirectly.voidsetParentScopeResolution(boolean parentScopeResolution)Given:voidsetPreEvaluatePartialBlocks(boolean preEvaluatePartialBlocks)If true, partial blocks will implicitly be evaluated before the partials will actually be executed.voidsetPrettyPrint(boolean prettyPrint)If true, unnecessary spaces and new lines will be removed from output.voidsetStartDelimiter(java.lang.String startDelimiter)Set the start delimiter.voidsetStringParams(boolean stringParams)If true, missing helper parameters will be resolve to their names.HandlebarsstartDelimiter(java.lang.String startDelimiter)Set the start delimiter.booleanstringParams()If true, missing helper parameters will be resolve to their names.HandlebarsstringParams(boolean stringParams)If true, missing helper parameters will be resolve to their names.static voidwarn(java.lang.String message)Log the given message as warn and format the message within the args.static voidwarn(java.lang.String message, java.lang.Object... args)Log the given message as warn and format the message within the args.Handlebarswith(TemplateCache cache)Set a newTemplateCache.Handlebarswith(EscapingStrategy escapingStrategy)Set a newEscapingStrategy.Handlebarswith(EscapingStrategy... chain)Set a newEscapingStrategy.Handlebarswith(Formatter formatter)Add a new variable formatter.Handlebarswith(HelperRegistry registry)Set the helper registry.Handlebarswith(TemplateLoader... loader)Set one or moreTemplateLoader.Handlebarswith(ParserFactory parserFactory)Set a newParserFactory.
-
-
-
Field Detail
-
DELIM_START
public static final java.lang.String DELIM_START
The default start delimiter.- See Also:
- Constant Field Values
-
DELIM_END
public static final java.lang.String DELIM_END
The default end delimiter.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Handlebars
public Handlebars(TemplateLoader loader)
Creates a newHandlebarswith no cache.- Parameters:
loader- The template loader. Required.
-
Handlebars
public Handlebars()
Creates a newHandlebarswith aClassPathTemplateLoaderand no cache.
-
-
Method Detail
-
precompile
public java.lang.String precompile(java.lang.String path)
Precompile a template to JavaScript.- Parameters:
path- Template path.- Returns:
- JavaScript.
-
precompileInline
public java.lang.String precompileInline(java.lang.String template)
Precompile a template to JavaScript.- Parameters:
template- Template.- Returns:
- JavaScript.
-
compile
public Template compile(java.lang.String location) throws java.io.IOException
Compile the resource located at the given uri. The implementation uses a cache for previously compiled Templates. By default, if the resource has been compiled previously, and no changes have occurred since in the resource, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
location- The resource's location. Required.- Returns:
- A compiled template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
compile
public Template compile(java.lang.String location, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
Compile the resource located at the given uri. The implementation uses a cache for previously compiled Templates. By default, if the resource has been compiled previously, and no changes have occurred since in the resource, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
location- The resource's location. Required.startDelimiter- The start delimiter. Required.endDelimiter- The end delimiter. Required.- Returns:
- A compiled template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
compileInline
public Template compileInline(java.lang.String input) throws java.io.IOException
Compile a handlebars template. The implementation uses a cache for previously compiled Templates. By default, if same input string has been compiled previously, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
input- The handlebars input. Required.- Returns:
- A compiled template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
compileInline
public Template compileInline(java.lang.String input, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
Compile a handlebars template. The implementation uses a cache for previously compiled Templates. By default, if same input string has been compiled previously, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
input- The input text. Required.startDelimiter- The start delimiter. Required.endDelimiter- The end delimiter. Required.- Returns:
- A compiled template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
compile
public Template compile(TemplateSource source) throws java.io.IOException
Compile a handlebars template. The implementation uses a cache for previously compiled Templates. By default, if the resource has been compiled previously, and no changes have occurred since in the resource, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
source- The template source. Required.- Returns:
- A handlebars template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
compile
public Template compile(TemplateSource source, java.lang.String startDelimiter, java.lang.String endDelimiter) throws java.io.IOException
Compile a handlebars template. The implementation uses a cache for previously compiled Templates. By default, if the resource has been compiled previously, and no changes have occurred since in the resource, compilation will be skipped and the previously created Template will be returned. You can set an alternate cache implementation usingwith.- Parameters:
source- The template source. Required.startDelimiter- The start delimiter. Required.endDelimiter- The end delimiter. Required.- Returns:
- A handlebars template.
- Throws:
java.io.IOException- If the resource cannot be loaded.
-
helper
public <C> Helper<C> helper(java.lang.String name)
Find a helper by name.- Specified by:
helperin interfaceHelperRegistry- Type Parameters:
C- The helper runtime type.- Parameters:
name- The helper's name. Required.- Returns:
- A helper or null if it's not found.
-
registerHelper
public <H> Handlebars registerHelper(java.lang.String name, Helper<H> helper)
Register a helper in the helper registry.- Specified by:
registerHelperin interfaceHelperRegistry- Type Parameters:
H- The helper runtime type.- Parameters:
name- The helper's name. Required.helper- The helper object. Required.- Returns:
- This handlebars.
-
registerHelperMissing
public <H> Handlebars registerHelperMissing(Helper<H> helper)
Register a missing helper in the helper registry.- Specified by:
registerHelperMissingin interfaceHelperRegistry- Type Parameters:
H- The helper runtime type.- Parameters:
helper- The helper object. Required.- Returns:
- This handlebars.
-
registerHelpers
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:- A method can/can't be static
- The method's name became the helper's name
- Context, parameters and options are all optional
- If context and options are present they must be the first and last method arguments.
- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
helperSource- The helper source. Required.- Returns:
- This handlebars object.
-
registerHelpers
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:- A method can/can't be static
- The method's name became the helper's name
- Context, parameters and options are all optional
- If context and options are present they must be the first and last method arguments.
Enums are supported too
- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
helperSource- The helper source. Enums are supported. Required.- Returns:
- This handlebars object.
-
registerHelpers
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.- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
location- A classpath location. Required.- Returns:
- This handlebars object.
- Throws:
java.lang.Exception- If the JavaScript helpers can't be registered.
-
registerHelpers
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.- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
input- A JavaScript file name. Required.- Returns:
- This handlebars object.
- Throws:
java.lang.Exception- If the JavaScript helpers can't be registered.
-
registerHelpers
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.- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
filename- The file name (just for debugging purpose). Required.source- The JavaScript source. Required.- Returns:
- This handlebars object.
- Throws:
java.lang.Exception- If the JavaScript helpers can't be registered.
-
registerHelpers
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.- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
filename- The file name (just for debugging purpose). Required.source- The JavaScript source. Required.- Returns:
- This handlebars object.
- Throws:
java.lang.Exception- If the JavaScript helpers can't be registered.
-
registerHelpers
public Handlebars registerHelpers(java.lang.String filename, java.lang.String source) throws java.io.IOException
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.- Specified by:
registerHelpersin interfaceHelperRegistry- Parameters:
filename- The file name (just for debugging purpose). Required.source- The JavaScript source. Required.- Returns:
- This handlebars object.
- Throws:
java.io.IOException- If the JavaScript helpers can't be registered.
-
helpers
public java.util.Set<java.util.Map.Entry<java.lang.String,Helper<?>>> helpers()
Description copied from interface:HelperRegistryList all the helpers from registry.- Specified by:
helpersin interfaceHelperRegistry- Returns:
- Available helpers in the registry.
-
getLoader
public TemplateLoader getLoader()
The resource locator.- Returns:
- The resource locator.
-
getCache
public TemplateCache getCache()
The template cache.- Returns:
- The template cache.
-
getEscapingStrategy
public EscapingStrategy getEscapingStrategy()
The escaping strategy.- Returns:
- The escaping strategy.
-
stringParams
public boolean stringParams()
If true, missing helper parameters will be resolve to their names.- Returns:
- If true, missing helper parameters will be resolve to their names.
-
prettyPrint
public boolean prettyPrint()
If true, unnecessary spaces and new lines will be removed from output. Default is: false.- Returns:
- If true, unnecessary spaces and new lines will be removed from output. Default is: false.
-
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
If true, unnecessary spaces and new lines will be removed from output. Default is: false.- Parameters:
prettyPrint- If true, unnecessary spaces and new lines will be removed from output. Default is: false.
-
prettyPrint
public Handlebars prettyPrint(boolean prettyPrint)
If true, unnecessary spaces and new lines will be removed from output. Default is: false.- Parameters:
prettyPrint- If true, unnecessary spaces and new lines will be removed from output. Default is: false.- Returns:
- This handlebars object.
-
setStringParams
public void setStringParams(boolean stringParams)
If true, missing helper parameters will be resolve to their names.- Parameters:
stringParams- If true, missing helper parameters will be resolve to their names.
-
stringParams
public Handlebars stringParams(boolean stringParams)
If true, missing helper parameters will be resolve to their names.- Parameters:
stringParams- If true, missing helper parameters will be resolve to their names.- Returns:
- The handlebars object.
-
infiniteLoops
public boolean infiniteLoops()
If true, templates will be able to call him self directly or indirectly. Use with caution. Default is: false.- Returns:
- If true, templates will be able to call him self directly or indirectly. Use with caution. Default is: false.
-
setInfiniteLoops
public void setInfiniteLoops(boolean infiniteLoops)
If true, templates will be able to call him self directly or indirectly. Use with caution. Default is: false.- Parameters:
infiniteLoops- If true, templates will be able to call him self directly or indirectly.
-
infiniteLoops
public Handlebars infiniteLoops(boolean infiniteLoops)
If true, templates will be able to call him self directly or indirectly. Use with caution. Default is: false.- Parameters:
infiniteLoops- If true, templates will be able to call him self directly or indirectly.- Returns:
- The handlebars object.
-
deletePartialAfterMerge
public boolean deletePartialAfterMerge()
If true, templates will be deleted once applied. Useful, in some advanced template inheritance use cases. Used by{{#block}} helper. Default is: false. At any time you can override the default setup with:{{#block "footer" delete-after-merge=true}}- Returns:
- True for clearing up templates once they got applied. Used by
{{#block}} helper.
-
deletePartialAfterMerge
public Handlebars deletePartialAfterMerge(boolean deletePartialAfterMerge)
If true, templates will be deleted once applied. Useful, in some advanced template inheritance use cases. Used by{{#block}} helper. Default is: false. At any time you can override the default setup with:{{#block "footer" delete-after-merge=true}}- Parameters:
deletePartialAfterMerge- True for clearing up templates once they got applied. Used by{{#block}} helper.- Returns:
- This handlebars object.
-
setDeletePartialAfterMerge
public void setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
If true, templates will be deleted once applied. Useful, in some advanced template inheritance use cases. Used by{{#block}} helper. Default is: false. At any time you can override the default setup with:{{#block "footer" delete-after-merge=true}}- Parameters:
deletePartialAfterMerge- True for clearing up templates once they got applied. Used by{{#block}} helper.
-
setEndDelimiter
public void setEndDelimiter(java.lang.String endDelimiter)
Set the end delimiter.- Parameters:
endDelimiter- The end delimiter. Required.
-
endDelimiter
public Handlebars endDelimiter(java.lang.String endDelimiter)
Set the end delimiter.- Parameters:
endDelimiter- The end delimiter. Required.- Returns:
- This handlebars object.
-
getEndDelimiter
public java.lang.String getEndDelimiter()
The End Delimiter.- Returns:
- The End Delimiter.
-
setStartDelimiter
public void setStartDelimiter(java.lang.String startDelimiter)
Set the start delimiter.- Parameters:
startDelimiter- The start delimiter. Required.
-
startDelimiter
public Handlebars startDelimiter(java.lang.String startDelimiter)
Set the start delimiter.- Parameters:
startDelimiter- The start delimiter. Required.- Returns:
- This handlebars object.
-
getStartDelimiter
public java.lang.String getStartDelimiter()
The Start Delimiter.- Returns:
- The Start Delimiter.
-
with
public Handlebars with(TemplateLoader... loader)
Set one or moreTemplateLoader. In the case of two or moreTemplateLoader, aCompositeTemplateLoaderwill be created. Default is:ClassPathTemplateLoader.- Parameters:
loader- The template loader. Required.- Returns:
- This handlebars object.
- See Also:
CompositeTemplateLoader
-
with
public Handlebars with(ParserFactory parserFactory)
Set a newParserFactory.- Parameters:
parserFactory- A parser factory. Required.- Returns:
- This handlebars object.
-
with
public Handlebars with(TemplateCache cache)
Set a newTemplateCache.- Parameters:
cache- The template cache. Required.- Returns:
- This handlebars object.
-
with
public Handlebars with(HelperRegistry registry)
Set the helper registry. This operation will override will remove any previously registered helper.- Parameters:
registry- The helper registry. Required.- Returns:
- This handlebars object.
-
with
public Handlebars with(EscapingStrategy escapingStrategy)
Set a newEscapingStrategy.- Parameters:
escapingStrategy- The escaping strategy. Required.- Returns:
- This handlebars object.
-
with
public Handlebars with(EscapingStrategy... chain)
Set a newEscapingStrategy.- Parameters:
chain- The escaping strategy. Required.- Returns:
- This handlebars object.
-
getFormatter
public Formatter.Chain getFormatter()
- Returns:
- A formatter chain.
-
with
public Handlebars with(Formatter formatter)
Add a new variable 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); } });- Parameters:
formatter- A formatter.- Returns:
- This handlebars object.
-
handlebarsJsFile
public Handlebars handlebarsJsFile(java.lang.String location)
Set the handlebars.js location used it to compile/precompile template to JavaScript.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 ishandlebars-v4.0.4.js.- Parameters:
location- A classpath location of the handlebar.js file.- Returns:
- This instance of Handlebars.
-
handlebarsJsFile
public java.lang.String handlebarsJsFile()
- Returns:
- Classpath location of the handlebars.js file. Default is:
handlebars-v4.0.4.js
-
parentScopeResolution
public boolean parentScopeResolution()
- Returns:
- True, if we want to extend lookup to parent scope, like Mustache Spec. Or false, if lookup is restricted to current scope, like handlebars.js.
-
setParentScopeResolution
public void setParentScopeResolution(boolean parentScopeResolution)
Given:{ "value": "Brett", "child": { "bestQB" : "Favre" } }Handlebars.java will output:Hello Favre Brettwhile 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.- Parameters:
parentScopeResolution- False, if we want to restrict lookup to current scope (like in handlebars.js). Default istrue
-
parentScopeResolution
public Handlebars parentScopeResolution(boolean parentScopeResolution)
Given:{ "value": "Brett", "child": { "bestQB" : "Favre" } }Handlebars.java will output:Hello Favre Brettwhile 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.- Parameters:
parentScopeResolution- False, if we want to restrict lookup to current scope (like in handlebars.js). Default istrue- Returns:
- This handlebars.
-
preEvaluatePartialBlocks
public boolean preEvaluatePartialBlocks()
If true, partial blocks will implicitly be evaluated before the partials will actually be executed. If false, you need to explicitly evaluate and render partial blocks with
Attention: If this is set to true, Handlebars works *much* slower! while rendering partial blocks. Default is: true for compatibility reasons.{{> @partial-block}}- Returns:
- If true partial blocks will be evaluated before the partial will be rendered to allow inline block side effects. If false, you will have to evaluate and render partial blocks explitly (this option is *much* faster).
-
setPreEvaluatePartialBlocks
public void setPreEvaluatePartialBlocks(boolean preEvaluatePartialBlocks)
If true, partial blocks will implicitly be evaluated before the partials will actually be executed. If false, you need to explicitly evaluate and render partial blocks with
Attention: If this is set to true, Handlebars works *much* slower! while rendering partial blocks. Default is: true for compatibility reasons.{{> @partial-block}}- Parameters:
preEvaluatePartialBlocks- If true partial blocks will be evaluated before the partial will be rendered to allow inline block side effects. If false, you will have to evaluate and render partial blocks explitly (this option is *much* faster).
-
preEvaluatePartialBlocks
public Handlebars preEvaluatePartialBlocks(boolean preEvaluatePartialBlocks)
If true, partial blocks will implicitly be evaluated before the partials will actually be executed. If false, you need to explicitly evaluate and render partial blocks with
Attention: If this is set to true, Handlebars works *much* slower! while rendering partial blocks. Default is: true for compatibility reasons.{{> @partial-block}}- Parameters:
preEvaluatePartialBlocks- If true partial blocks will be evaluated before the partial will be rendered to allow inline block side effects. If false, you will have to evaluate and render partial blocks explitly (this option is *much* faster).- Returns:
- The Handlebars object
-
getParserFactory
public ParserFactory getParserFactory()
Return a parser factory.- Returns:
- A parser factory.
-
log
public static void log(java.lang.String message, java.lang.Object... args)Log the given message and format the message within the args.- Parameters:
message- The log's message.args- The optional args.- See Also:
String.format(String, Object...)
-
log
public static void log(java.lang.String message)
Log the given message and format the message within the args.- Parameters:
message- The log's message.- See Also:
String.format(String, Object...)
-
warn
public static void warn(java.lang.String message, java.lang.Object... args)Log the given message as warn and format the message within the args.- Parameters:
message- The log's message.args- The optional args.- See Also:
String.format(String, Object...)
-
warn
public static void warn(java.lang.String message)
Log the given message as warn and format the message within the args.- Parameters:
message- The log's message.- See Also:
String.format(String, Object...)
-
debug
public static void debug(java.lang.String message, java.lang.Object... args)Log the given message as debug and format the message within the args.- Parameters:
message- The log's message.args- The optional args.- See Also:
String.format(String, Object...)
-
debug
public static void debug(java.lang.String message)
Log the given message as debug and format the message within the args.- Parameters:
message- The log's message.- See Also:
String.format(String, Object...)
-
error
public static void error(java.lang.String message, java.lang.Object... args)Log the given message as error and format the message within the args.- Parameters:
message- The log's message.args- The optional args.- See Also:
String.format(String, Object...)
-
error
public static void error(java.lang.String message)
Log the given message as error and format the message within the args.- Parameters:
message- The log's message.- See Also:
String.format(String, Object...)
-
decorator
public Decorator decorator(java.lang.String name)
Description copied from interface:HelperRegistryFind a decorator by name.- Specified by:
decoratorin interfaceHelperRegistry- Parameters:
name- A decorator's name.- Returns:
- A decorator or
null.
-
registerDecorator
public Handlebars registerDecorator(java.lang.String name, Decorator decorator)
Description copied from interface:HelperRegistryRegister a decorator and make it accessible viaHelperRegistry.decorator(String).- Specified by:
registerDecoratorin interfaceHelperRegistry- Parameters:
name- A decorator's name. Required.decorator- A decorator. Required.- Returns:
- This registry.
-
setCharset
public Handlebars setCharset(java.nio.charset.Charset charset)
Description copied from interface:HelperRegistrySet the charset to use.- Specified by:
setCharsetin interfaceHelperRegistry- Parameters:
charset- Charset.- Returns:
- This registry.
-
getCharset
public java.nio.charset.Charset getCharset()
- Returns:
- Charset.
-
-