express-pagelet

Pagelets to render parts of your response afterwards

npm install express-pagelet
16 downloads in the last month

express-pagelet

  1. Configure the middleware for express:

     app.use(require('express-pagelet').createMiddleware({
       pageletDirectory: path.join(__dirname, 'pagelets')
     }));
    
  2. 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);
     };
    
  3. Example view-file in views/pagelet/foo.jade:

     h2= heading
     p= content
     p
       strong= page
    
  4. 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' });
     };
    
  5. The pagelets are available in your views as locals:

     !{pagelet_foo}
     !{pagelet_bar}
    
npm loves you