简体   繁体   中英

Global variable for Jade templates in node.js

I am using node.js with Jade templating system.

Assume, I have these routing rules:

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

And these dummy views:

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

How can I declare author variable generally, for both views?

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

or

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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