Comparing version 2.0.2 to 2.1.0
@@ -9,28 +9,22 @@ | ||
// expose handlebars, allows users to use their versions | ||
// by overriding this early in their apps | ||
exports.handlebars = require('handlebars'); | ||
var Instance = function() { | ||
// expose handlebars, allows users to use their versions | ||
// by overriding this early in their apps | ||
// express 2.x template engine compliance | ||
exports.compile = function (str, options) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
this.handlebars = require('handlebars').create(); | ||
var template = exports.handlebars.compile(str); | ||
return function (locals) { | ||
return template(locals, { | ||
helpers: locals.blockHelpers, | ||
partials: null, | ||
data: null | ||
}); | ||
}; | ||
}; | ||
// cache for templates, express 3.x doesn't do this for us | ||
this.cache = {}; | ||
// cache for templates, express 3.x doesn't do this for us | ||
var cache = exports.cache = {}; | ||
this.__express = middleware.bind(this); | ||
// DEPRECATED, kept for backwards compatibility | ||
this.SafeString = this.handlebars.SafeString; | ||
this.Utils = this.handlebars.Utils; | ||
} | ||
// express 3.x template engine compliance | ||
exports.__express = function(filename, options, cb) { | ||
var handlebars = exports.handlebars; | ||
function middleware(filename, options, cb) { | ||
var cache = this.cache; | ||
var handlebars = this.handlebars; | ||
@@ -145,14 +139,28 @@ // grab extension from filename | ||
/// expose useful methods | ||
// express 2.x template engine compliance | ||
Instance.prototype.compile = function (str, options) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
exports.registerHelper = function () { | ||
exports.handlebars.registerHelper.apply(exports.handlebars, arguments); | ||
var template = this.handlebars.compile(str); | ||
return function (locals) { | ||
return template(locals, { | ||
helpers: locals.blockHelpers, | ||
partials: null, | ||
data: null | ||
}); | ||
}; | ||
}; | ||
exports.registerPartial = function () { | ||
exports.handlebars.registerPartial.apply(exports.handlebars, arguments); | ||
Instance.prototype.registerHelper = function () { | ||
this.handlebars.registerHelper.apply(this.handlebars, arguments); | ||
}; | ||
exports.registerAsyncHelper = function(name, fn) { | ||
exports.handlebars.registerHelper(name, function(context) { | ||
Instance.prototype.registerPartial = function () { | ||
this.handlebars.registerPartial.apply(this.handlebars, arguments); | ||
}; | ||
Instance.prototype.registerAsyncHelper = function(name, fn) { | ||
this.handlebars.registerHelper(name, function(context) { | ||
return async.resolve(fn, context); | ||
@@ -162,4 +170,5 @@ }); | ||
// DEPRECATED, kept for backwards compatibility | ||
exports.SafeString = exports.handlebars.SafeString; | ||
exports.Utils = exports.handlebars.Utils; | ||
module.exports = new Instance(); | ||
module.exports.create = function() { | ||
return new Instance(); | ||
}; |
{ | ||
"name": "hbs", | ||
"description": "Express.js template engine plugin for Handlebars", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/donpark/hbs", | ||
@@ -16,4 +16,4 @@ "author": "Don Park <donpark@docuverse.com> (http://blog.docuverse.com)", | ||
"mocha": "1.6.0", | ||
"npm": "~1.1.62", | ||
"request": "~2.9.202" | ||
"npm": "1.2.14", | ||
"request": "2.16.6" | ||
}, | ||
@@ -20,0 +20,0 @@ "scripts": { |
@@ -6,8 +6,4 @@ # hbs [![Build Status](https://secure.travis-ci.org/donpark/hbs.png)](http://travis-ci.org/donpark/hbs) # | ||
## Why ## | ||
## Install ## | ||
Because Handlebars.js is a nifty and simple templating language and makes a great fit for express templates. | ||
## Installation ## | ||
``` | ||
@@ -17,3 +13,3 @@ npm install hbs | ||
## Usage ## | ||
## Use ## | ||
@@ -46,8 +42,7 @@ Using *hbs* as the default view engine requires just one line of code in your app setup. This will render `.hbs` files when `res.render` is called. | ||
## handlebars | ||
## handlebars ## | ||
The handlebars require used by hbs can be accessed via the `handlebars` property on the `hbs` module. | ||
If you wish to use handlebars methods like `SafeString` please do so on this property. Do not register | ||
helpers or partials in this way. | ||
If you wish to use handlebars methods like `SafeString` please do so on this property. Do not register helpers or partials in this way. | ||
@@ -61,4 +56,18 @@ ``` | ||
### extra scripts or styles | ||
### more than one instance ### | ||
You can create isolated instances of hbs using the `create()` function on the module object. | ||
``` | ||
var hbs = require('hbs'); | ||
var instance1 = hbs.create(); | ||
var instance2 = hbs.create(); | ||
app.engine('html', instance1.__express); | ||
app.engine('hbs', instance2.__express); | ||
``` | ||
Each instance has the same methods/properties as the `hbs` module object. The module object is actually just an instance created for you automatically. | ||
### extra scripts or styles ## | ||
Sometimes it is useful to have custom scripts or stylesheets on your pages. Handlebars does not provide a way to import or extend a template, but through the use of helpers you can create a similar result. | ||
@@ -65,0 +74,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13189
267
75