Express 3和EJS中的布局
Express版本3中删除了一些function:
the concept of a "layout" (template engine specific now) partial() (template engine specific)
更新日志: https : //github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
partial()
可以改为EJS自己的叫做include
的特性,但是布局的替代方法是什么?
从Express 3看来,布局function是委托给模板引擎的责任。 您可以使用ejs-locals( https://github.com/RandomEtc/ejs-locals )进行布局。
安装ejs-locals
npm install ejs-locals --save
在app.js中使用ejs-locals作为你的app引擎
var express = require('express'); var engine = require('ejs-locals'); ... app.engine('ejs', engine); app.set('view engine', 'ejs');
现在你可以使用布局
layout.ejs <body> <%- body %> </body> index.ejs <% layout('layout') -%> <div class="container"> <div class="jumbotron"> ...
另一个select是使用expression式部分( https://github.com/publicclass/express-partials )。 两人做同样的事情,所以这只是你的select。
我也为此而挣扎。 所以我用一个ejs和dustjs的例子来搭build一个github项目。
https://github.com/chovy/express-template-demo
我不确定partial和include之间的区别,你不需要明确地将数据传递给include。 不知道为什么你会想要一个部分。
但是对于一个布局,你只需要像这样指定一个块:
//layout.ejs <html> <%- body %> </html> //page1.ejs <% layout('layout') -%> This is loaded from page 1 and overrides <%- body %> in the layout.ejs.
如果有人想添加更多的例子,只需提交一个拉请求。
您可以使用“包含”选项在Express 2.x中模拟EJS布局。 在这里看到我的答案: