Layouts in Express 3 and EJS


In version 3 of Express some features were removed:

the concept of a "layout" (template engine specific now)
partial() (template engine specific)


The partial() can be changed for EJS own feature called include, but what is the alternative for layouts?

9/27/2012 8:02:45 AM

Accepted Answer

It seems that from Express 3, layout feature is delegated to the responsibility of template engines. You can use ejs-locals ( for layout.

Install ejs-locals

npm install ejs-locals --save

Use ejs-locals as your app engine in app.js

var express = require('express');
var engine = require('ejs-locals');

app.engine('ejs', engine);
app.set('view engine', 'ejs');

Now you can use layout

  <%- body %>

<% layout('layout') -%>

<div class="container">
<div class="jumbotron">

Another option is to use express-partials ( The two do the same thing, so it's just your choice.

2/6/2014 12:58:49 PM

I struggled with this as well. So I put up a github project with an example for ejs and dustjs.

I'm not sure the difference between a partial and an include, you don't need to explicitly pass data to an include. Not sure why you would want a partial.

But for a layout, you just specify a block like this:

<%- body %>

<% layout('layout') -%>
This is loaded from page 1 and overrides <%- body %> in the layout.ejs.

If anyone wants to add more examples, just submit a pull request.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow