Handlebars.compile(template, options)
var template = Handlebars.compile('{{foo}}'); template({});
data
: Set to false to disable @data
tracking. compat
: Set to true to enable recursive field lookup. knownHelpers
: Hash containing list of helpers that are known to exist (truthy) at template execution time. Passing this allows the compiler to optimize a number of cases. Builtin helpers are automatically included in this list and may be omitted by setting that value to false
. knownHelpersOnly
: Set to true to allow further optimzations based on the known helpers list. noEscape
: Set to true to not HTML escape any content. strict
: Run in strict mode. In this mode, templates will throw rather than silently ignore missing fields. This has the side effect of disabling inverse operations such as {{^foo}}{{/foo}}
unless fields are explicitly included in the source object. assumeObjects
: Removes object existence checks when traversing paths. This is a subset of strict
mode that generates optimized templates when the data inputs are known to be safe. preventIndent
: By default, an indented partial-call causes the output of the whole partial being indented by the same amount. This can lead to unexpected behavior when the partial writes pre
-tags. Setting this option to true
will disable the auto-indent feature. ignoreStandalone
: Disables standalone tag removal when set to true
. When set, blocks and partials that are on their own line will not remove the whitespace on that line. explicitPartialContext
: Disables implicit context for partials. When enabled, partials that are not passed a context value will execute against an empty object. Handlebars.precompile(template, options)
var templateSpec = Handlebars.precompile('{{foo}}');
Handlebars.compile
method. Additionally may pass: srcName
: Passed to generate the source map for the input file. When run in this manner, the return structure is {code, map}
with code
containing the template definition and map
containing the source map. destName
: Optional parameter used in conjunction with srcName
to provide a destination file name when generating source maps. Handlebars.template(templateSpec)
Handlebars.precompile
. var template = Handlebars.template(templateSpec); template({});
Handlebars.registerPartial(name, partial)
Handlebars.registerPartial('foo', partial);
Handlebars.registerPartial({ foo: partial, bar: partial });
Handlebars.template
method. Handlebars.unregisterPartial(name)
Handlebars.unregisterPartial('foo');
Handlebars.registerHelper(name, helper)
Handlebars.registerHelper('foo', function() { });
Handlebars.registerHelper({ foo: function() { }, bar: function() { } });
Handlebars.unregisterHelper(name)
Handlebars.unregisterHelper('foo');
Handlebars.registerDecorator(name, helper)
Handlebars.registerDecorator('foo', function() { });
Handlebars.registerDecorator({ foo: function() { }, bar: function() { } });
Handlebars.unregisterDecorator(name)
Handlebars.unregisterDecorator('foo');
Handlebars.SafeString(string)
string
from being escaped when the template is rendered. new Handlebars.SafeString('<div>HTML Content!</div>')
Handlebars.escapeExpression
method to avoid potential security concerns. Handlebars.escapeExpression(string)
Handlebars.Utils.escapeExpression(string)
&
, <
, >
, "
, '
, `
, =
with the HTML entity equivalent value for string values. SafeString
values are left untouched. SafeString
instance, to prevent possible code injection. Handlebars.Utils.escapeExpression
. Handlebars.createFrame(data)
if (options.data) { var data = Handlebars.createFrame(options.data); data.foo = 'bar'; options.data = data; }
each
iterator creates a single frame which is reused for all child execution. Handlebars.create()
var OtherHandlebars = Handlebars.create();
Handlebars
environment directly. Handlebars.template
for each environment. This applies to partials as well. Handlebars.noConflict()
var myHandlebars = Handlebars.noConflict();
Handlebars.log(level, message)
log
helper. Handlebars.Utils
object. Handlebars.Utils.isEmpty(value)
Handlebars.Utils.isEmpty(value)
if
and with
helpers to control their execution flow. The Handlebars definition of empty is any of: Handlebars.Utils.extend(obj, value)
obj
with all keys defined on value
. Handlebars.Utils.extend(foo, {bar: true})
bar
on object foo
with the value true
. Handlebars.Utils.toString(obj)
toString
method. Handlebars.Utils.isArray(obj)
Handlebars.Utils.isFunction(obj)
@data
variables are implemented by Handlebars and its builtin helpers. @root
{{#each array}} {{@root.foo}} {{/each}}
@first
each
helper for the first step of iteration. {{#each array}} {{#if @first}} First! {{/if}} {{/each}}
@index
each
helper. {{#each array}} {{@index}} {{/each}}
@key
each
helper when iterating over objects. {{#each array}} {{@key}} {{/each}}
@last
each
helper for the last step of iteration. {{#each array}} {{#if @last}} Last :( {{/if}} {{/each}}
@level
template({}, {data: {level: Handlebars.logger.WARN}})
Handlebars.logger.DEBUG
, Handlebars.logger.INFO
, Handlebars.logger.WARN
, or Handlebars.logger.ERROR
Handlebars.logger.level
or higher. The default value is Handlebars.logger.ERROR
.
© 2011–2017 by Yehuda Katz
Licensed under the MIT License.
https://handlebarsjs.com/reference.html