Skip to content

Add SafeString to context (or use duck-typing) #886

Closed
@dominicbarnes

Description

@dominicbarnes

Hey there, I'm having trouble with one aspect of using handlebars in a node setting.

When I want to share helpers from various places, and I want to use handlebars.SafeString, I am finding it difficult, since the instanceof check will only pass if the SafeString object came from the local instance. With node, there could be several versions of handlebars installed, but only the local one passes.

Passing around a single instance of handlebars to all of these modules is not ideal, especially for such a trivial thing. I have a few proposals (in order of preference/ease-of-implementation)

  1. add SafeString to the context of the helper, so I don't need any other external reference for it to work:
exports.myHelper = function (options) {
  return new options.SafeString("foo");
};
  1. use duck-typing within Utils.escapeExpression, rather than (or in addition to) instanceof:
} else if (typeof string === "object" && typeof string.string === "string") {
  // this test may not be accurate, this is just an example
}
  1. execute helpers using the handlebars instance as this
exports.myHelper = function () {
  return new this.SafeString("foo");
};

For now, I can just use {{{ to accomplish what I need. So, this is not "urgent", but I would like to start the discussion. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions