0

我刚刚发现 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'});

布局.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>  
4

1 回答 1

2

折腾了一下,app.use(partials());需要放在前面app.use(app.router);,否则layout.ejs加载不出来。

于 2013-10-30T03:27:07.763 回答