用于node.js中Jade模板的全局变量 [英] Global variable for Jade templates in node.js

查看:276
本文介绍了用于node.js中Jade模板的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 node.js Jade 模板系统。

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

  // ./routes/first.js 

exports.first = function(req,res)
{
res.render('first',{
author:'Edward',
title:'First页面'
});
};

// ./routes/second.js

exports.second = function(req,res)
{
res.render('second ',{
作者:'Edward',
标题:'第二页'
});
};

以及这些虚拟视图:

  // ./views/first.jade 

html
head
title#{author}& ndash; #{title}
正文
第一页内容

// ./views/second.jade

html
头部
title#{author}& ndash; #{title}
body
span第二页内容

如何声明 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 
头部
标题Edward& ndash; #{title}

// ./views/first.jade

html
include include / head
body
span第一页内容

// ./views/second.jade

html
包含包含/首部
主体
第二页内容

  // ./views/layout.jade 
html
head
title Edward& ndash; #{title}
正文
正文

// ./views/first.jade

扩展布局
追加正文
span首页内容

// ./views/second.jade

扩展布局
追加正文
跨页第二页内容


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

这篇关于用于node.js中Jade模板的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆