我有一個測試應用程序運行在快遞3.x上,用EJS代替了視圖(而不是玉),我能夠創建佈局並相應地使用它們。但最近遷移到表達4.x,除佈局外,一切都很好。看起來佈局沒有正確加載或者根本沒有加載。快遞4.x(遷移)上的EJS佈局
是否有某種需要加載的新模塊?我錯過了什麼?我努力將編碼編碼到我的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(路由器調用「 /約')
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向後兼容,但不確定這是否與您的問題有關。 – Elisabeth 2015-05-18 22:19:38