在 Express 4.x 中我使用以下内容加载ejs:
var path = require('path');
// Set the default templating engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// The views/index.ejs exists in the app directory
app.get('/hello', function (req, res) {
res.render('index', {title: 'title'});
});
那么你只需要两个文件就可以了——views/index.ejs:
<%- include partials/navigation.ejs %>
还有views/partials/navigation.ejs:
<ul><li class="active">...</li>...</ul>
您也可以告诉 Express 使用 ejs 来作为 html 模板:
var path = require('path');
var EJS = require('ejs');
app.engine('html', EJS.renderFile);
// Set the default templating engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// The views/index.html exists in the app directory
app.get('/hello', function (req, res) {
res.render('index.html', {title: 'title'});
});
最后你也可以使用ejs布局模块:
var EJSLayout = require('express-ejs-layouts');
app.use(EJSLayout);
这将使用views/layout.ejs 作为您的布局。