Question

I am using Node.js and ExpressJS 3.0. Moving to 3.0, I found that partials were no longer supported and subsequently found express-partials to provide similar functionality.

Looking at the example on the GitHub page, I find this:

app.get('/',function(req,res,next){
  res.render('index.ejs') 
  // -> render layout.ejs with index.ejs as `body`.
})

This is fine, but what if I have a layout that depends on multiple partials? That is, what if I have a block in the layout.ejs file for the header, one for the content, and one for the footer? For example, what if I use one template file for the entire web application, but different kinds of users have different headers, content blocks, and footers?

Looking at the limited documentation of express-partials, I cannot find this functionality. I was expecting something like this:

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
});

How can I achieve this functionality?

Was it helpful?

Solution

I'd suggest you to use ejs includes + switches

Sorry, I'm not familliar with ejs syntax, so – jade, but the essense is the same:

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"
....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top