Node Express Vars传递给jade模板

时间:2013-11-19 11:11:38

标签: node.js express pug

我将我的vars传递给jade时遇到了问题:

app.js

var express = require('express');
var http = require('http');
var path = require('path');


var app = express();

// Eviroment Variables
app.set('port', process.env.PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

在我的服务经理中:

app.get('/view/users', function(req, res) {
        if (req.session.username === null || req.session.username === undefined){
    // if user is not logged-in redirect back to login page //
            res.redirect('/');
        }
        else{
            userProvider.fetchAllUsers(function(error, users) {
                console.log(users);
                res.render('portal/users', {locals:{
                        udata : req.session.username,
                        users : users
                }});
            });
        }
    });

控制台输出看起来如此:

[ { title: 'Herr',
givenname: 'xxx',
lastname: 'xxx',
name: 'xxx',
username: 'xxx',
password: 'TcVUEcbQfcb35d7dbadd5f0cc7836a06bd170a91d9',
email: 'xxx',
phone: '0000-000000',
fax: '0000-000000',
lastlogin: '23.03.1987',
state: '1',
createdate: 'November 15th 2013, 1:19:15 pm',
_id: 528611433891166f07000001 } ]

我的Jade模板如下:

tbody
    - var users = [{_id:'', name:'', username:'', email:'', phone:'', fax:'', lastlogin:'', createdate:'', state:''}]
    - each user in users
          tr
              td= user._id
              td= user.name
              td= user.username
              td= user.email
              td= user.phone
              td= user.fax
              td= user.lastlogin
              td= user.createdate
              td= user.state

我的用户将不会被显示。我之前使用页面标题对其进行了测试,但没有成功。

谢谢!

编辑: 我现在添加以下内容:

res.render('portal/users', {
                            users : users
                    }); 

到我的经理并将其添加到我的玉石模板中:

tbody
                    - if (users.length) {
                    - users.forEach(function(user) {
                    tr
                        td= user._id
                        td= user.name
                        td= user.username
                        td= user.email
                        td= user.phone
                        td= user.fax
                        td= user.lastlogin
                        td= user.createdate
                        td= user.state
                    - });}

一切都很完美。

1 个答案:

答案 0 :(得分:0)

您可以使用jade对each

的直接支持进一步简化
tbody
each user in users
  tr
    td= user._id
    ...