express-pagelet
v0.1.3
Published
Pagelets to render parts of your response afterwards
Downloads
4
Readme
express-pagelet
Configure the middleware for express:
app.use(require('express-pagelet').createMiddleware({ pageletDirectory: path.join(__dirname, 'pagelets') }));
Define a pagelet, e.g. in
pagelets/foo.js
:var FooPagelet = module.exports = function() {}; FooPagelet.prototype.load = function(opts, ready) { this.page = opts.page || 1; // load from db etc. ready(); }; FooPagelet.prototype.render = function(req, res, done) { return res.render('pagelet/foo.jade', { heading: 'Foo pagelet', content: 'Some more content...', page: this.page }, done); };
Example view-file in
views/pagelet/foo.jade
:h2= heading p= content p strong= page
In your route-handler add pagelets, either lazy loading (async: true) or rendering as part of the request:
exports.index = function(req, res) { res.addPagelet('foo', { async: true }); // type defaults to the name of pagelet res.addPagelet('bar', { type: 'foo' }); res.renderWithPagelets('index', { title: 'Express' }); };
The pagelets are available in your views as locals:
!{pagelet_foo} !{pagelet_bar}