handlebars api

2019-03-05  本文已影响1人  全栈弄潮儿

Handlebars.compile(template, options)

Compiles a template so it can be executed immediately.

var template = Handlebars.compile('{{foo}}’);
template({});

Supports a variety of options that alter how the template executes.

Handlebars.precompile(template, options)

Precompiles a given template so it can be sent to the client and executed without compilation.

var templateSpec = Handlebars.precompile('{{foo}}’);

Supports all of the same options parameters as the Handlebars.compile method. Additionally may pass:

Handlebars.template(templateSpec)

Sets up a template that was precompiled with Handlebars.precompile.

var template = Handlebars.template(templateSpec);
template({});

Handlebars.registerPartial(name, partial)

Registers partials accessible by any template in the environment.

Handlebars.registerPartial('foo', partial);

Also supports registering multiple partials at once.

Handlebars.registerPartial({
  foo: partial,
  bar: partial
});

If loading the whole library, the partials may be string values which will be compiled on demand. If only loading the runtime, the partials must be a precompiled template that has been set up properly using the Handlebars.template method.

Handlebars.unregisterPartial(name)

Unregisters a previously registered partial.

Handlebars.unregisterPartial('foo’);

Handlebars.registerHelper(name, helper)

Registers helpers accessible by any template in the environment.

Handlebars.registerHelper('foo', function() {
});

Also supports registering multiple helpers at once.

Handlebars.registerHelper({
  foo: function() {
  },
  bar: function() {
  }
});

Handlebars.unregisterHelper(name)

Unregisters a previously registered helper.

Handlebars.unregisterHelper('foo’);

Handlebars.registerDecorator(name, helper)

Registers a decorator accessible by any template in the environment.

Handlebars.registerDecorator('foo', function() {
});

Also supports registering multiple decorators at once.

Handlebars.registerDecorator({
  foo: function() {
  },
  bar: function() {
  }
});

Handlebars.unregisterDecorator(name)

Unregisters a previously registered decorator.

Handlebars.unregisterDecorator('foo’);

Handlebars.SafeString(string)

Prevents string from being escaped when the template is rendered.

new Handlebars.SafeString('<div>HTML Content!</div>’)

When constructing the string that will be marked as safe, any external content should be properly escaped using the Handlebars.escapeExpression method to avoid potential security concerns.

Handlebars.escapeExpression(string)

HTML escapes the passed string, making it safe for rendering as text within HTML content.

Handlebars.Utils.escapeExpression(string)

Replaces &, <, >, ", ', ```, = with the HTML entity equivalent value for string values. SafeStringvalues are left untouched.

The output of all expressions except for triple-braced expressions are passed through this method. Helpers should also use this method when returning HTML content via a SafeString instance, to prevent possible code injection.

This method is aliased at Handlebars.Utils.escapeExpression.

Handlebars.createFrame(data)

Used by block helpers to create child data objects.

if (options.data) {
  var data = Handlebars.createFrame(options.data);
  data.foo = ‘bar’;
  options.data = data;
}

Helpers that modify the data state should create a new frame when doing so, to isolate themselves and avoid corrupting the state of any parents. Generally, only one frame needs to be created per helper execution. For example, the each iterator creates a single frame which is reused for all child execution.

Handlebars.create()

Creates an isolated Handlebars environment.

var OtherHandlebars = Handlebars.create();

Each environment has its own helpers and partials. This is only necessary for use cases that demand distinct helpers or partials. Most use cases can use the root Handlebars environment directly.

Templates created for a given environment are bound to that environment. This means that templates that need to run in multiple environments will need to be recompiled or reconstructed via Handlebars.template for each environment. This applies to partials as well.

Handlebars.noConflict()

Removes this Handlebars instance from the global namespace, restoring the global Handlebars variable to its previous value.

var myHandlebars = Handlebars.noConflict();

This allows for distinct versions of the library to be used simultaneously without concern for version conflicts.

Handlebars.log(level, message)

Logger used by the log helper.

May be overriden if desired.

Utilities

Handlebars offers a variety of utility methods that are exposed through the Handlebars.Utilsobject.

Handlebars.Utils.isEmpty(value)

Determines if a given value is empty.

Handlebars.Utils.isEmpty(value)

This is used by the built-in if and with helpers to control their execution flow. The Handlebars definition of empty is any of:

This is intended to match the Mustache behavior.

Handlebars.Utils.extend(obj, value)

Simple utility method to augment obj with all keys defined on value.

Handlebars.Utils.extend(foo, {bar: true})

Will set the key bar on object foo with the value true.

Handlebars.Utils.toString(obj)

Generic toString method.

Handlebars.Utils.isArray(obj)

Determines if an object is an array.

Handlebars.Utils.isFunction(obj)

Determines if an object is a function.

@data Variables

The following @data variables are implemented by Handlebars and its builtin helpers.

@root

Initial context with which the template was executed.

{{#each array}}
  {{@root.foo}}
{{/each}}

Unless explicitly modified, this value is consistent across all portions of the page rendering, meaning it can be used within partials where depthed parameters are unable to reference their parent templates.

@first

Set to true by the each helper for the first step of iteration.

{{#each array}}
  {{#if @first}}
    First!
  {{/if}}
{{/each}}

@index

Zero-based index for the current iteration step. Set by the each helper.

{{#each array}}
  {{@index}}
{{/each}}

@key

Key name for the current iteration step. Set by the each helper when iterating over objects.

{{#each array}}
  {{@key}}
{{/each}}

@last

Set to true by the each helper for the last step of iteration.

{{#each array}}
  {{#if @last}}
    Last :(
  {{/if}}
{{/each}}

@level

Assigned log level.

template({}, {data: {level: Handlebars.logger.WARN}})

May be set to one of Handlebars.logger.DEBUG , Handlebars.logger.INFO ,Handlebars.logger.WARN , or Handlebars.logger.ERROR

When set, the logger will include in its output only messages with a log level ofHandlebars.logger.level or higher. The default value is Handlebars.logger.ERROR.

上一篇 下一篇

猜你喜欢

热点阅读