我正在使用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'
});
};
这些虚拟观点:
// ./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
对于两种观点,我如何一般性地声明author
变量?
答案 0 :(得分:2)
// ./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