【问题标题】:Ejs-partial problems with express3 layouts not workingexpress3 布局的 Ejs 部分问题不起作用
【发布时间】:2013-11-09 02:34:00
【问题描述】:

我刚刚发现 express 3 破坏了 ejs 布局功能,所以我尝试使用 https://github.com/publicclass/express-partials 编写一些代码,这应该将布局功能重新添加到 express 中,但是我的布局文件中的 标记是无论我尝试多少,都不会被指定的 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>  

【问题讨论】:

    标签: node.js express ejs


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2020-11-11
      相关资源
      最近更新 更多