1

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

4

1 回答 1

2

Jade 不支持条件布局切换

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

这将呈现具有布局的页面,而与变量名称无关。

方法一

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

布局.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/浏览器请求。

方法二

另一种简洁的方法是修改你的 layout.jade 本身的条件

布局.jade

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

并从您的路由器传递变量,同时每次呈现相同的页面:

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' });
});
于 2016-05-24T03:27:22.010 回答