Layouts support for EJS templates in Express 3+.

Usage no npm install needed!

<script type="module">
  import expressEjsExtend from '';


Express EJS Extend

Layouts support for EJS templates in Express 3+.


First, add it as engine in your app:

// server.js

var express = require('express'),
    path    = require('path');

var app = express();

app.engine('ejs', require('express-ejs-extend')); // add this line
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.get('/', function (req, res) {
  res.render('index', {title: 'world!'});


Finally, in your views, call the extend function:

<%# views/index.ejs %>

<% extend('layout') %>

<h1>Hello <%= title %></h1>
<%# views/layout.ejs %>

<!DOCTYPE html>
<%- content %>

That's all, your GET / will render:

<!DOCTYPE html>
<h1>Hello world!</h1>


  • In your view (the one that calls extend), you can surround the extend with any combination of (<% or <%-) and (%> or -%>).
  • In your layout, it's recommended to use exactly <%- content %> so the engine doesn't escape your HTML tags. Also you can end it with -%> instead to avoid any trailing \n.
  • The signature of extend is: extend(layout[, data]), so you can also pass an object with data and that will be passed to the layout.


This project is released under the MIT license.