【问题标题】:How to pass a variable to the footer in Jade using Node.js如何使用 Node.js 将变量传递到 Jade 中的页脚
【发布时间】:2023-03-07 09:48:03
【问题描述】:

我想在页脚中使用名为 total 的玉显示一个变量。

.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

但是,我无法弄清楚如何使用 Node 传递它。

我使用...将变量传递给页面

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

但我不明白如何为页脚布局做到这一点。

我会做这样的事情吗...

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

如果是这样,我如何在 app.js 文件中定义该路由?

【问题讨论】:

    标签: javascript node.js express pug


    【解决方案1】:

    您传递给res.render('index') 的任何变量也应该可用于页脚视图。

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

    如果你想让一个变量在所有视图中都可用,你可以使用 express locals:http://expressjs.com/api.html#app.locals

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-18
      • 2011-08-29
      • 2015-11-24
      • 2018-09-10
      • 1970-01-01
      • 2014-10-27
      • 2017-01-27
      • 1970-01-01
      相关资源
      最近更新 更多