Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6a40b21
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = require('./lib/express-handlebars'); | ||
exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "*" | ||
} | ||
} |