【发布时间】:2015-04-08 02:15:14
【问题描述】:
我有一个在 express 3.x 上运行的测试应用程序,使用 EJS 作为视图(而不是玉),我能够创建布局并相应地使用它们。但最近迁移到 express 4.x 并且除了布局之外一切都很好。似乎布局没有被正确加载或根本没有加载。
是否有一些新的模块需要加载?我错过了什么?我尝试将布局硬编码到我的 res.render 参数上,但没有采用。我什至为应用程序本身设置了默认布局。
./views/layouts/layout.ejs(所有视图的默认布局
<!DOCTYPE html>
<html>
<head>
<% include partials/head %>
</head>
<body>
<% include partials/top_sidebar %>
<%- body %>
</body>
<% include partials/footer %>
</html>
./views/partials/top_sidebar.ejs
<a href = "/" >Home</a>
<a href = "/about" >About</a>
<a href = "/contact" >Contact</a>
<% if (!isAuthenticated) { %>
<a href = "/login" >Login</a>
<% } else { %>
<a href = "/user" >Profile</a>
<a href = "/" >Jobs</a>
<a href = "/logout" >Logout</a>
<% } %>
app.js(路由器调用'/about')
router.get('/about', function(req, res, next){
res.render('about', { isAuthenticated: false, user: req.user, title: 'About' });
});
./views/about.ejs
<h1><%= title %></h1>
<p>Something that gives about information.</p>
在这个示例代码中,about 视图在没有 top_sidebar 的情况下加载。因此我假设布局没有正确加载。即使我强迫它这样做:res.render('about', { isAuthenticated: false, user: req.user, title: 'About', layout: "./layouts/layout.ejs" });
【问题讨论】:
-
会不会是与 EJS 版本不兼容有关?我看到 EJS 2 不向后兼容 EJS1,但不确定这是否与您的问题有关。
标签: node.js express migration ejs