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
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.
This seems to be an issue that's still open on github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With