ejs <%- body %> 不工作
ejs <%- body %> not working
我将使用名为 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>
<% } %>
模板引擎不工作,没有绑定两个文件。我需要在 express 中进行一些额外的配置才能做到这一点?
我正在使用 express 4.*
谢谢。
这是我过去用来在 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);
};
}
express 4.x不支持布局,可以使用模块"express-ejs-layouts"来解决这个问题
npm install express-ejs-layouts
在您的代码中,请包含:
var expressLayouts=require("express-ejs-layouts");
app.use(expressLayouts);
我将使用名为 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>
<% } %>
模板引擎不工作,没有绑定两个文件。我需要在 express 中进行一些额外的配置才能做到这一点?
我正在使用 express 4.*
谢谢。
这是我过去用来在 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);
};
}
express 4.x不支持布局,可以使用模块"express-ejs-layouts"来解决这个问题
npm install express-ejs-layouts
在您的代码中,请包含:
var expressLayouts=require("express-ejs-layouts");
app.use(expressLayouts);