How to completely redefine `res.render` method?

47 views
Skip to first unread message

Alexander Baumgertner

unread,
Feb 5, 2015, 8:55:57 AM2/5/15
to sai...@googlegroups.com
I would like to completely redefine res.render method: my template engine is not compatible with consolidate.


I decided to use hook for it, but can't understand, how I can to write my own realization of res.render.

Any thoughts or suggestions?

Thanks!

Alexander Baumgertner

unread,
Feb 5, 2015, 9:09:43 AM2/5/15
to sai...@googlegroups.com

Alexander Baumgertner

unread,
Feb 28, 2015, 2:53:41 PM2/28/15
to sai...@googlegroups.com
So, I think, I figured out this questions: http://stackoverflow.com/questions/28346085/how-to-completely-redefine-res-render-method-in-salsjs/28786103#28786103


You can do the same as here lib/hooks/views/index.js (in your sails hook)

sails.on('router:before', function() {
    sails.router.bind('/*', function(req, res, next) {
        // here you can reimplement the res.render method, of example:
         res.render = function render(data) {
             // For access `req` data in your template engine,
             // many useful info stored here like:
             // req.options.controller – current `controller`
             // req.options.action – current `action`
             data.req = req;

             var html = yourTemplateEngineRender(data);

             res.set({
                 'Content-Type': 'text/html',
                 'Content-Length': Buffer.byteLength(html, 'utf8')
             });

             res.send(html);
         } 
    }, 'all');
});

See my hook which enables bem rendering for sails: sails-hook-bem-render

Reply all
Reply to author
Forward
0 new messages