javascript - node.js 中 Jade 模板的全局变量

标签 javascript node.js templates global-variables pug

我正在使用 node.jsJade 模板系统。

假设,我有这些路由规则:

// ./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'
    });
};

还有这些虚拟 View :

// ./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

如何为两个 View 声明一般的 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'
    });
};

// ./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

关于javascript - node.js 中 Jade 模板的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12088557/

相关文章:

c++ - 无限模板递归,因为仅使用 gcc 没有 bool 表达式优化

javascript - 从浏览器控制台调用 Angular 单击事件

Javascript - 将电子邮件验证功能添加到现有验证功能

javascript - 调整 javascript 正则表达式以过滤 url 中的变量

node.js - Express Passport failureRedirect 不工作

Node.js:Mongoose 模式默认随机标记不是随机的

C++ 隐式/显式模板方法特化问题

javascript - 使用 Node Clone 重复 HTML/PHP 代码

javascript - 如何解决转发消息的问题?

c++ - 构造函数中的模板冲突