在一个布局上使用Express-Partials的多个部分

时间:2013-07-14 02:53:51

标签: javascript node.js express partials

我正在使用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
});

如何实现此功能?

1 个答案:

答案 0 :(得分:1)

我建议你使用ejs includes + switches

对不起,我不熟悉ejs语法,所以 - 玉,但是本质是一样的:

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"
....
相关问题