Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs jade conditional extend

I would like to make my Jade page extend different layouts, depending on a condition. So my code looks like this:

if myConditionVariable
  extends layout1
else
  extends layout2  
block content
  p here goes my content!

Now this does not work. It seems as if only the last defined extends will be respected, regardless of the conditions. I also tried dynamically defining the templatename, such as

extends myLayoutNameVariable

and set the myLayoutNameVariable in different manners (express dynamic helper function, set it as var, local var etc...)

Is there any other solution for conditional layouts or can someone tell me what i am doing wrong?

Cheers, simon

like image 490
cmonsqpr Avatar asked Jun 14 '12 20:06

cmonsqpr


2 Answers

So, a slightly less intrusive way to deal with this is to change the layout itself.

I added a bit of middleware, and the rest flows..

app.use(function(req, res, next){
    res.locals.isAjax = req.headers['x-requested-with'] && 
        req.headers['x-requested-with'] === 'XMLHttpRequest';
    next();
});

Now that this is set, "isAjax" is available to jade.

In my "layout.jade" (the template you will extend from), I do this:

- if(!isAjax)
  doctype 5
    html
      head
      body
        block content
        block scripts
- else
  block content
  block scripts

I have stripped out the other elements of the full layout.jade for (hopefully) clarity.

Finally, my normal views that extend layout.jade work without modification (this jade template includes both cases).

extends layout.jade
  .container.
     This is text will be wrapped for a non-ajax request, 
     but not for an ajax request.
like image 177
Andrew Theken Avatar answered Oct 07 '22 05:10

Andrew Theken


This seems to be an issue that's still open on github.

like image 32
Pickels Avatar answered Oct 07 '22 03:10

Pickels