Express3布局的Ejs部分问题不起作用

时间:2013-10-29 18:45:26

标签: node.js express ejs

我刚刚发现express 3打破了ejs布局功能,所以我试图用https://github.com/publicclass/express-partials编写一些代码,这应该重新添加布局功能到express,但是<% - body%>无论我尝试多少,我的布局文件中的标签永远不会被指定的ejs文件替换 继承人我的app.js

var express = require('express')
, partials = require('express-partials');
var mainscreen = require('./routes/mainscreen');

var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = module.exports = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(partials());
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}
app.get('/', mainscreen.mainscreen);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

这是我的路线

exports.mainscreen = function(req, res, next){
  res.render('index.ejs', { 
                        title:'Title',
                        titleinfobar: 'Title',
                        accountinfobar: 'Not signed in',
                        bodycontent: 'Body of the doc'});

layout.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
        <%- body %>     
  </body>               
</html>  

和index.ejs

<div class="head">
        <div class="col-big"><div id="righttitle"><%=titleinfobar %></div></div>
        <div class="col-small"><div id="lefttitle"><%=accountinfobar %></div></div>
</div>
<div class="contentbody">
        <div id="content">
                <%= bodycontent %>
        </div>          
</div>  

1 个答案:

答案 0 :(得分:2)

稍微修补一下后,需要在app.use(partials());之前放置app.use(app.router);,否则不会加载layout.ejs。