我将使用名为layout.ejs的默认模板,这是文件中的代码:
<html>
<head>
<title></title>
</head>
<body>
<%- body %>
</body>
</html>
现在我正在尝试将此文件与我的new.ejs绑定,这是在我的事件文件夹中:
<% if (parseInt(page['prevPage'].charAt(0)) === 0) { %>
<a type="button" class="btn btn-default" href="/admin/bookings/<%= page.nextPage %>">Próximo ></a>
<% } else { %>
<a type="button" class="btn btn-default" href="/admin/bookings/<%= page.prevPage %>">< Anterior</a>
<a type="button" class="btn btn-default" href="/admin/bookings/<%= page.nextPage %>">Próximo ></a>
<% } %>
模板引擎无法正常工作,不绑定这两个文件。我需要一些额外的配置才能做到这一点吗?
我正在使用快递4。*
感谢。
答案 0 :(得分:1)
这是我过去用过的一个猴子补丁,用于在Express 4中使用ejs布局:
/*
Usage:
Set a global/default layout with:
app.set('view layout', 'foo');
Set a layout per-render (overrides global layout) with:
res.render('foo', { layout: 'bar' });
Or disable a layout if a global layout is set with:
res.render('foo', { layout: false });
If no layout is provided using either of the above methods,
then the view will be rendered as-is like normal.
Inside your layout, the variable `body` holds the rendered partial/child view.
Installation:
Call `mpResponse();` before doing `require('express');` in your application.
*/
function mpResponse() {
var expressResponse = require('express/lib/response'),
expressResRender = expressResponse.render;
expressResponse.render = function(view, options, fn) {
options = options || {};
var self = this,
req = this.req,
app = req.app,
layout,
cb;
// support callback function as second arg
if (typeof options === 'function')
fn = options, options = {};
// merge res.locals
options._locals = self.locals;
// default callback to respond
fn = fn || function(err, str) {
if (err) return req.next(err);
self.send(str);
};
if (typeof options.layout === 'string')
layout = options.layout;
else if (options.layout !== false
&& typeof app.get('view layout') === 'string')
layout = app.get('view layout');
if (layout) {
cb = function(err, str) {
if (err) return req.next(err);
options.body = str;
expressResRender.call(self, layout, options, fn);
};
} else
cb = fn;
// render
app.render(view, options, cb);
};
}
答案 1 :(得分:0)
express 4.x不支持布局,可以使用模块“ express-ejs-layouts”来解决此问题
npm install express-ejs-layouts
在您的代码中,请包含:
var expressLayouts=require("express-ejs-layouts");
app.use(expressLayouts);