我正在将node.js
与Jade
模板系统一起使用。
假设我有以下路由规则:
// ./routes/first.js
exports.first = function(req, res)
{
res.render('first', {
author: 'Edward',
title: 'First page'
});
};
// ./routes/second.js
exports.second = function(req, res)
{
res.render('second', {
author: 'Edward',
title: 'Second page'
});
};
这些虚拟 View :
// ./views/first.jade
html
head
title #{author} – #{title}
body
span First page content
// ./views/second.jade
html
head
title #{author} – #{title}
body
span Second page content
我如何才能在两种 View 中一般都声明
author
变量? 最佳答案
// ./author.js
module.exports = 'Edward';
// ./routes/first.js
exports.first = function(req, res)
{
res.render('first', {
author: require('../author'),
title: 'First page'
});
};
// ./routes/second.js
exports.second = function(req, res)
{
res.render('second', {
author: require('../author'),
title: 'Second page'
});
};
或者
// ./views/includes/head.jade
head
title Edward – #{title}
// ./views/first.jade
html
include includes/head
body
span First page content
// ./views/second.jade
html
include includes/head
body
span Second page content
或者
// ./views/layout.jade
html
head
title Edward – #{title}
body
block body
// ./views/first.jade
extends layout
append body
span First page content
// ./views/second.jade
extends layout
append body
span Second page content
关于javascript - node.js中Jade模板的全局变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12088557/