layouts in expressjs

20,520

I usually set layout to false globally, so I know exactly what layout I use where (so no default layout):

app.set('view options', { layout: false });

Then in my routes I can set a layout per route like so:

res.render('my_page', { layout: 'my_layout' });

Read more about Express layouts and templates engines

Share:
20,520
Huy Tran
Author by

Huy Tran

Updated on July 22, 2020

Comments

  • Huy Tran
    Huy Tran almost 4 years

    I want to use 2 layouts for main page and admin page
    What should i configure my code to do that?
    here is my current code configure

    app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(require('stylus').middleware({ src: __dirname + '/public' }));
    
    app.use(express.static(__dirname + '/public'));
    app.use(express.cookieParser());
    app.use(express.session({secret: 'secrect', store: MemStore({
      reapInterval: 60000 * 10
    })}));
    app.use(app.router);
    });
    
  • alessioalex
    alessioalex over 12 years
  • chovy
    chovy almost 12 years
    expressjs.com link no longer goes to layout.
  • bendytree
    bendytree over 11 years
    Here's the layout docs for 2.X expressjs.com/2x/guide.html#view-rendering. It looks like layouts disappeared in 3.X and were replaced by includes? github.com/visionmedia/express/blob/master/examples/ejs/view‌​s/…
  • Orkun Balkancı
    Orkun Balkancı over 10 years
    For express 3.x Layout & Partial support check out github.com/publicclass/express-partials
  • Tien Do
    Tien Do almost 9 years
    Actually I can't find any page saying about rendering on epxressjs.com now, document is simple and hard to find information. Do they want all us use their training services? :)
  • colecmc
    colecmc over 5 years
    Exactly what I needed