【发布时间】:2016-09-20 08:51:11
【问题描述】:
如果请求是通过 ajax 发出的,我只想从 page1.jade 发送块内容,如果是正常的 GET,它应该使用 layout.jade 中内置的块来回答
【问题讨论】:
-
@avck33 这不是重点。我正在寻找使用单个模板文件的方法。
标签: javascript node.js pug pugjs
如果请求是通过 ajax 发出的,我只想从 page1.jade 发送块内容,如果是正常的 GET,它应该使用 layout.jade 中内置的块来回答
【问题讨论】:
标签: javascript node.js pug pugjs
翡翠不支持conditional layout switch:
if type=='get'
extends layout
block content
p This is block content
这将使用布局呈现页面,而与变量名称无关。
方法一
一种简单的方法是在单独的文件中定义块内容并将其包含在您的 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
另一种 cleaner 方法是修改你的 layout.jade 本身以进行条件
layout.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' });
});
【讨论】: