Description #
EJS is a simple templating language that lets you generate HTML markup using plain JavaScript. It is widely used in Node.js environments for rendering server-side HTML views, often with frameworks like Express.js. EJS allows you to embed JavaScript logic directly into your HTML templates.
History #
EJS was created as a lightweight alternative to other templating engines like Jade (now Pug) and Handlebars. It gained popularity with the rise of Express.js, offering a minimal, logic-friendly syntax. Unlike other template engines, EJS favors raw JavaScript over domain-specific syntax, which appeals to developers familiar with JS.
Key milestones:
- Released in the early 2010s
- Became the default templating engine in many Express.js tutorials
- Continues to be maintained as part of the open-source Node ecosystem
- Used in early server-rendered web apps before React and Vue became popular
Hello World Code #
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello, <%= name %>!</h1>
</body>
</html>
How to Run #
With Node.js and Express:
- Create a simple Express server:
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index', { name: 'World' });
});
app.listen(3000, () => console.log('Server running on port 3000'));
- Create a file named
views/index.ejs
with the Hello World code above. - Run with:
node app.js
Key Concepts #
<%= %>
outputs escaped content<%- %>
outputs unescaped HTML<% %>
runs JS logic (loops, conditions)- Supports template inheritance using includes (
<%- include('header') %>
) - Variables passed in via
res.render()
- Files usually placed in a
/views
directory - Works with Express.js, Fastify, and other frameworks
- Useful for server-side rendering (SSR)
- Syntax mirrors HTML for readability
- Lightweight and dependency-free
Try It Online #
Fun Facts #
- EJS was one of the earliest templating engines used in Express apps
- It doesn’t abstract JavaScript—developers can use full JS inside templates
- EJS is still maintained and stable, even though frontend frameworks now dominate
- Works well with legacy and small server-rendered apps
- It’s often used in quick prototyping and dashboard backends