我使用的是Node.js和ExpressJS 3.0。迁移到3.0版本后,我发现partials不再受支持,随后发现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
});如何实现此功能?
发布于 2013-07-14 19:00:42
我建议您使用ejs include+开关
对不起,我不太熟悉ejs语法,so - 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"
....https://stackoverflow.com/questions/17636111
复制相似问题