Handlebars - Miscellaneous

handlebars

What is the purpose of the option object?

The template function can be passed an options object as the second parameter which allows for customization. For example:

var options = {};
var context = {title: "My New Post", body: "This is my first post!"}
var html    = template(context, options);
  • data: Pass in an object to define custom @variable private variables.
  • helpers: Pass in to provide custom helpers in addition to the globally defined helpers. Values defined in this object will replace any values defined in the global object for the duration of the template execution.
  • partials: Pass in to provide custom partials in addition to the globally defined partials. Values defined in this object will replace any values defined in the global object for the duration of the template execution.

NEED CONCRETE EXAMPLE.

What is the purpose of Handlebars.createFrame(data)?

Used by block helpers to create a child data object.

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 isolates themselves and avoid corrupting the state of any parents. Generally only one frame needs to be created per helper execution, i.e. the each iterator only creates one frame which is reused for all child execution.

NEED CONCRETE EXAMPLE SHOWING THE SITUATION THAT WE NEED TO USE THIS.

What is the purpose of Handlebars.create()?

Creates an isolated Handlebars environment.

var OtherHandlebars = Handlebars.create();

Each environment has it's 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.

What is the purpose of Handlebars.noConflict()?

Removes this Handlebars instance from the global space, restoring any libraries that may have been previously registered.

var myHandlebars = Handlebars.noConflict();

This allows for distinct versions of the library to be loaded into one global space without concern for potential version conflicts.

What is the purpose of the @contextPath variable?

Value set in trackIds mode that records the lookup path for the current context.

Any custom helpers that apply a custom context should set this value as appropriate. If the new context is the child of an existing one, it may use the Handlebars.Utils.appendContextPath helper to perform this operation.

if (options.data && options.ids) {
  var data = Handlebars.createFrame(options.data);
  data.contextPath = Handlebars.Utils.appendContextPath(data.contextPath, 'foo');
  options.data = data;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License