Node.js Express - how to get partial views asynchronously

I have a layout menu - navigation. In express tutorials, only pages from the old school are loaded. the whole old page is thrown away, and the new one is loaded with all layouts, views and partial views. And I want the navigation menu to stay. So how can I do this?

If I may be mistaken in this web page architecture, please help me.

+4
source share
2 answers

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

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

If you want to search for Ajax requests as opposed to full-page browser downloads, use the req.xhr flag as described here

So you can even do

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

You can also use res.partial (), which is intended for partial allocation.

Here is an example of its use, where "browse.jade" is the 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 } }); }); }; 
0
source

All Articles