Node.js,Jade / Pug,如何提供全部和部分模板?

如果通过ajax发出请求,我想只发送来自page1.jade的块内容,如果是正常的GET,它应该回答这个内置到layout.jade中的块

Jade不支持条件布局切换 :

if type=='get' extends layout block content p This is block content 

这将与布局呈现页面,而不考虑variables名称。

方法1

一个简单的方法是在一个单独的文件中定义块内容,并将其包含在page1.jade中,然后可以独立地访问该块。

layout.jade

 html head title My Site - #{title} block scripts body block content block foot 

page1.jade

 extends layout block content include ./includes/block.jade 

包括/ block.jade

 p This is the block content 

这将是处理您的路线文件中的请求的方式

 router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); router.get('/block', function(req, res, next) { res.render('includes/block', { title: 'Express' }); }); 

修改它以处理AJAX /浏览器请求。

方法2

其他更清洁的方式将修改您的layout.jade本身的条件

layout.jade

 if type=='get' html head title My Site - #{title} block scripts body block content block foot 

每次渲染同一页面时,从路由器传递variables:

 router.get('/', function(req, res, next) { res.render('index', { title: 'Express',type:'get' }); }); router.get('/block', function(req, res, next) { res.render('index', { title: 'Block Express' }); });