如何在不传递给每条路线的情况下为主车把布局设置变量? [英] How to set a variable for the main handlebars layout without passing it to every route?
本文介绍了如何在不传递给每条路线的情况下为主车把布局设置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有nodejs和express的车把.这是我的main.handlebars文件:
I'm using handlebars with nodejs and express. This is my main.handlebars file:
<!doctype html>
<html>
<head>
...
</head>
<body>
<div class ="container">
...
<footer>
© {{copyrightYear}} Meadowlark Travel
</footer>
</div>
</body>
</html>
到目前为止,我已经将版权年度传递到了每条路线:
So far I'm passing the copyright year to every route:
var date = new Date();
var copyrightYear = date.getFullYear();
app.get(
'/',
function( req, res) {
res.render(
'home',
{
copyrightYear: copyrightYear
}
);
}
);
是否可以全局设置copyrightYear
变量,所以我不必将其传递给每条路线/视图?
Is it possible to set the copyrightYear
variable globally, so I don't have to pass it on to every route/view?
推荐答案
ExpressJS提供了某种全局变量".在文档中提到了它们: app.locals .要将其包含在每个响应中,您可以执行以下操作:
ExpressJS provides some kind of "global variables". They are mentioned in the docs: app.locals. To include it in every response you could do something like this:
app.locals.copyright = '2014';
这篇关于如何在不传递给每条路线的情况下为主车把布局设置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文