Comparing version 1.0.1 to 1.0.2
100
lib/hbs.js
@@ -1,9 +0,17 @@ | ||
var handlebars = require('handlebars'); | ||
var compile = function (str, options) { | ||
// builtin | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
// expose handlebars, allows users to use their versions | ||
// by overriding this early in their apps | ||
exports.handlebars = require('handlebars'); | ||
// express 2.x template engine compliance | ||
exports.compile = function (str, options) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
var template = handlebars.compile(str); | ||
var template = exports.handlebars.compile(str); | ||
return function (locals) { | ||
@@ -18,19 +26,87 @@ return template(locals, { | ||
// Express view template engine compliance | ||
// cache for templates, express 3.x doesn't do this for us | ||
var cache = {}; | ||
exports.compile = compile; | ||
// express 3.x template engine compliance | ||
exports.__express = function(filename, options, cb) { | ||
var handlebars = exports.handlebars; | ||
// Expose Handlebars and useful methods | ||
// render the original file | ||
// cb(err, str) | ||
function render_file(locals, cb) { | ||
// cached? | ||
var template = cache[filename]; | ||
if (template) { | ||
return cb(null, template(locals)); | ||
} | ||
exports.handlebars = handlebars; | ||
fs.readFile(filename, 'utf8', function(err, str){ | ||
if (err) { | ||
return cb(err); | ||
} | ||
var locals = options; | ||
var template = handlebars.compile(str); | ||
if (options.cache) { | ||
cache[filename] = template; | ||
} | ||
cb(null, template(locals)); | ||
}); | ||
} | ||
// render with a layout | ||
function render_with_layout(template, locals, cb) { | ||
render_file(locals, function(err, str) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var locals = options; | ||
locals.body = str; | ||
var str = template(locals); | ||
cb(null, str); | ||
}); | ||
} | ||
// options.layout specified and false (don't use layout) | ||
if (options.layout !== undefined && !options.layout) { | ||
return render_file(options, cb); | ||
} | ||
var view_dir = options.settings.views; | ||
var layout_filename = path.join(view_dir, options.layout || 'layout.hbs'); | ||
var layout_template = cache[layout_filename]; | ||
if (layout_template) { | ||
return render_with_layout(layout_template, cb); | ||
} | ||
// TODO check if layout path has .hbs extension | ||
fs.readFile(layout_filename, 'utf8', function(err, str) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var layout_template = handlebars.compile(str); | ||
if (options.cache) { | ||
cache[layout_filename] = layout_template; | ||
} | ||
render_with_layout(layout_template, options, cb); | ||
}); | ||
} | ||
/// expose useful methods | ||
exports.registerHelper = function () { | ||
handlebars.registerHelper.apply(handlebars, arguments); | ||
exports.handlebars.registerHelper.apply(exports.handlebars, arguments); | ||
}; | ||
exports.registerPartial = function () { | ||
handlebars.registerPartial.apply(handlebars, arguments); | ||
exports.handlebars.registerPartial.apply(exports.handlebars, arguments); | ||
}; | ||
exports.SafeString = handlebars.SafeString; | ||
exports.Utils = handlebars.Utils; | ||
// DEPRECATED, kept for backwards compatibility | ||
exports.SafeString = exports.handlebars.SafeString; | ||
exports.Utils = exports.handlebars.Utils; |
{ | ||
"name": "hbs", | ||
"description": "Express.js template engine plugin for Handlebars", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"homepage": "https://github.com/donpark/hbs", | ||
@@ -14,2 +14,10 @@ "author": "Don Park <donpark@docuverse.com> (http://blog.docuverse.com)", | ||
}, | ||
"devDependencies": { | ||
"mocha": ">=1.0.0", | ||
"npm": "*", | ||
"request": ">=2.9.202" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"engines": { | ||
@@ -16,0 +24,0 @@ "node": ">= 0.4.0" |
@@ -7,2 +7,4 @@ # hbs # | ||
[![Build Status](https://secure.travis-ci.org/donpark/hbs.png)](http://travis-ci.org/donpark/hbs) | ||
## Why ## | ||
@@ -13,6 +15,8 @@ | ||
adds Express.js view engine support directly. | ||
## Installation ## | ||
npm install hbs | ||
``` | ||
npm install hbs | ||
``` | ||
@@ -22,8 +26,10 @@ ## Usage ## | ||
To set *hbs* as default view engine: | ||
app.set("view engine", "hbs"); | ||
```javascript | ||
app.set("view engine", "hbs"); | ||
``` | ||
## Examples ## | ||
See `test/server` source code. | ||
See `test/2.x/app.js` or `test/3.x/app.js` depending on your version of express. | ||
@@ -30,0 +36,0 @@ ## Issues ## |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
47
0
5767
3
7
89
1