简体   繁体   English

如何使用Node.js将变量传递给Jade中的页脚

[英]How to pass a variable to the footer in Jade using Node.js

I have a variable I'd like to display in my footer using jade called total . 我有一个变量,我想在我的页脚中使用称为total的 jade显示。

.container
    .footer
        hr(style='margin: 30px 0 10px 0;')
        p #{total} records in this database.

link(rel='stylesheet', href='/css/style.css')
script(src='/scripts/bootstrap.min.js')

block footer

However, I'm having trouble figuring out how to pass that in using Node. 但是,我无法弄清楚如何在使用Node时传递它。

I get passing a variable to a page using... 我使用...将变量传递给页面

exports.index = function (req, res) {
    "use strict";
    res.render('index', { title: 'My Title' });
};

But I'm not understanding how I can do this for the footer layout. 但我不明白我如何为页脚布局做到这一点。

Would I do something like this... 我会做这样的事......

res.render('footer', { total: 147});

if so how do I define that route in my app.js file? 如果是这样,我如何在app.js文件中定义该路线?

Any variables that you pass into res.render('index') should be available to the footer view as well. 您传递到res.render('index')任何变量也应该可用于页脚视图。

res.render('index', {title: 'My Title', total: 147});

If you want to make a variable available in all views, you can use express locals: http://expressjs.com/api.html#app.locals 如果要在所有视图中提供变量,可以使用快速本地化: http//expressjs.com/api.html#app.locals

// Put this where you configure your app
app.locals.total = 147

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM