Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jneen committed Mar 25, 2012
0 parents commit 6a40b21
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
# MIT license. See http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2012 Jay Adkisson.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@
module.exports = require('./lib/express-handlebars');
exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version;
50 changes: 50 additions & 0 deletions lib/express-handlebars.js
@@ -0,0 +1,50 @@
var Handlebars = require('handlebars');

module.exports = function(app) {
// *.hbs templates are rendered with Handlebars
app.register('hbs', Handlebars);

// hook into express's native helpers function to register helpers
// with Handlebars
var oldHelpers = app.helpers.bind(app)
app.helpers = function(h) {
Object.keys(h).forEach(function(k) {
Handlebars.registerHelper(k, h[k]);
});

return oldHelpers(h);
}

// Handlebars doesn't really do dynamic helpers,
// so we kind of hack them on here.
var oldDynamicHelpers = app.dynamicHelpers.bind(app);

// first, use the native dynamicHelpers to add the request and response
// objects to the rendering context
oldDynamicHelpers({
__req: function(req, res) { return req; },
__res: function(req, res) { return res; }
});

// then, when you register a dynamic helper, replace it with a function
// that uses the context's cache of (req, res) to create your helpers.
// the contract is that the dynamicHelper definition function is only
// called once per request.
app.dynamicHelpers = function(dh) {
const CACHE_KEY = '[[ dynamic helpers ]]';
Object.keys(dh).forEach(function(k) {
Handlebars.registerHelper(k, function() {
var self = this;

function getHelper(k) {
var cache = self.__res[CACHE_KEY] = self.__res[CACHE_KEY] || {}
return (cache[k] = cache[k] || dh[k](self.__req, self.__res));
}

return getHelper(k).apply(self, arguments);
});
});

return oldDynamicHelpers(dh);
};
};
15 changes: 15 additions & 0 deletions package.json
@@ -0,0 +1,15 @@
{
"name": "express-handlebars",
"version": "0.0.1",
"description": "handlebars.js integration with express",
"keywords": ["handlebars", "express", "helpers", "dynamicHelpers", "templates"],
"author": "Jay Adkisson <jjmadkisson at gmail dot com>",
"repository": "git://github.com/jayferd/express-handlebars",

"files": ["index.js", "lib", "package.json", "LICENSE"],
"main": "index.js",

"dependencies": {
"handlebars": "*"
}
}

0 comments on commit 6a40b21

Please sign in to comment.