Skip to content

Instantly share code, notes, and snippets.

@ozten
Last active March 11, 2017 15:11
Show Gist options
  • Save ozten/0b34b6325159081863e9 to your computer and use it in GitHub Desktop.
Save ozten/0b34b6325159081863e9 to your computer and use it in GitHub Desktop.
Adapter layer for bridging `handlebars-layouts` and `express-handlebars`.
/**
* Adapter layer for bridging `handlebars-layouts` and `express-handlebars`.
*
* Usage:
* var exphbs = require('express-handlebars');
* var handlebarsLayout = require('handlebars-layouts');
* var exphbsAdapter = require('express-handlebars-layouts-adapter');
*
* handlebarsLayout(exphbsAdapter);
* var exphbs.create({
* helpers: handlebarsLayout(exphbsAdapter()).helpers()
* });
* app.engine('handlebars', hbs.engine);
*
* WUT?
*
* It is not easy to use `handlebars-layouts` with `express-handlebars`.
*
* handlebars-layouts API
*
* layouts(handlebars); // calls handlebars.registerHelper(helpers);
* // then returns handlebars
*
* express-handlebars API
* var express = require('express'),
* exphbs = require('express-handlebars');
*
* var app = express();
*
* var hbs = exphbs.create({
* // Specify helpers which are only registered on this instance.
* helpers: {
* foo: function () { return 'FOO!'; },
* bar: function () { return 'BAR!'; }
* }
* });
*
* app.engine('handlebars', hbs.engine);
* app.set('view engine', 'handlebars');
*/
module.exports = function() {
var _helpers = {};
return {
registerHelper: function registerHelper(helpers) {
_helpers = helpers;
},
helpers: function() {
return _helpers;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment