[英]Multiple Partials Using Express-Partials On One Layout
我正在使用Node.js和ExpressJS 3.0。 转到3.0,我发现不再支持局部函数,随后发现Express-Partials提供了类似的功能。
查看GitHub页面上的示例,我发现:
app.get('/',function(req,res,next){
res.render('index.ejs')
// -> render layout.ejs with index.ejs as `body`.
})
很好,但是如果我的布局依赖于多个局部元素怎么办? 也就是说,如果我在layout.ejs
文件中有一个块用于标题,一个用于内容,一个用于页脚,该怎么办? 例如,如果我对整个Web应用程序使用一个模板文件,但是不同类型的用户具有不同的页眉,内容块和页脚怎么办?
查看express-partials
的有限文档,我找不到此功能。 我期待这样的事情:
app.get('/', function (req, res) {
res.render({header: 'header1.ejs', content: 'some_file.ejs', footer: 'footer1.ejs'});
// -> render layout.ejs with header1.ejs as `header`, some_file.ejs as `content, and footer.ejs as `footer
});
如何实现此功能?
我建议您使用ejs包含+开关
抱歉,我不熟悉ejs语法,所以– jade,但本质上是相同的:
app.get('/', function (req, res) {
res.render('index', {
header: 'header1',
, content: 'content1',
, footer: 'footer1'
});
});
index.jade
===========
//- header
case header
when "header1":
include "includes/header1"
when "header2":
include "includes/header2"
...
case content
when "content1":
include "includes/some_file1"
when "content2":
include "includes/some_file2"
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.