node.js Express - How to get partial views asynchronously node.js Express - How to get partial views asynchronously express express

node.js Express - How to get partial views asynchronously


As @drachenstern said, you want to render only partial HTML fragments, not whole documents including the layout. You can tell express to skip the layout using:

res.render('sometemplate', {layout: false});

If you want to look for Ajax requests as distinct from full-page browser loads, use the req.xhr flag as documented here

Thus you might even be able to do

res.render('sometemplate', {layout: !req.xhr});


You can also use res.partial() which is specifically for rendering partials.

Here is a sample of its usage, where 'browse.jade' is name of the template:

exports.browse = function(req, res){  var Contact = mongoose.model('Contact');  Contact.where({}).asc('surname', 'given_name', 'org').run(function(err, results) {    res.partial('browse', {         locals: { data: results }    });  });};