node.js Express - How to get partial views asynchronously

前端 未结 2 2082
借酒劲吻你
借酒劲吻你 2021-02-06 13:50

I\'ve got a layout - navigation menu. In express tutorials theres only old-school pages loading. whole old page is thrown away and a new one is downloaded with all layouts,views

2条回答
  •  灰色年华
    2021-02-06 13:58

    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});
    

提交回复
热议问题