Edw*_*its 5 javascript templates global-variables node.js pug
我使用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'
});
};
Run Code Online (Sandbox Code Playgroud)
这些虚拟视图:
// ./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
Run Code Online (Sandbox Code Playgroud)
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'
});
};
Run Code Online (Sandbox Code Playgroud)
或者
// ./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
Run Code Online (Sandbox Code Playgroud)
或者
// ./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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6903 次 |
| 最近记录: |