codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

7 / 76 topics
7Using Templates with Express.js8EJS (Embedded JavaScript) Templates9Pug (formerly Jade) Templates10Handlebars Templates
Tutorials/Express.js/Using Templates with Express.js
🚂Express.js

Using Templates with Express.js

Updated 2026-05-15
10 min read

Using Templates with Express.js

Introduction

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. One of the key aspects of building dynamic web applications is rendering views or templates. Express supports various templating engines, allowing developers to choose the one that best fits their needs.

In this tutorial, we will explore how to integrate three popular templating engines with Express.js: EJS (Embedded JavaScript), Pug (formerly Jade), and Handlebars. Each of these engines has its own syntax and features, making them suitable for different types of projects.

Concept

A templating engine is a tool that allows you to generate HTML dynamically by embedding expressions within static text. This is particularly useful for creating reusable templates that can be rendered with different data.

Express.js provides middleware to integrate various templating engines seamlessly. By using these engines, you can separate your application's logic from its presentation layer, making your code cleaner and more maintainable.

Examples

1. EJS (Embedded JavaScript)

EJS is a simple templating language that lets you generate HTML with plain JavaScript. It uses <% %> for control flow, <%= %> to output expressions, and <%- %> to output unescaped expressions.

Installation

First, install the EJS package using npm:

Terminal

Navigate to http://localhost:3000 in your browser, and you should see the rendered HTML with the data passed from the server.

Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h1>Welcome to Express.js with EJS!</h1>
</body>
</html>

2. Pug (formerly Jade)

Pug is a high-performance templating engine that compiles into HTML. It uses indentation to define the structure of the document, making it concise and easy to read.

Installation

Install the Pug package using npm:

Terminal

Navigate to http://localhost:3000 in your browser, and you should see the rendered HTML with the data passed from the server.

Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h1>Welcome to Express.js with Pug!</h1>
</body>
</html>

3. Handlebars

Handlebars is a logic-less templating engine that compiles into HTML. It uses double curly braces {{ }} for embedding expressions and supports helpers for more complex operations.

Installation

Install the Handlebars package using npm:

Terminal

Navigate to http://localhost:3000 in your browser, and you should see the rendered HTML with the data passed from the server.

Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h1>Welcome to Express.js with Handlebars!</h1>
</body>
</html>

What's Next?

In this tutorial, we explored how to integrate three popular templating engines (EJS, Pug, and Handlebars) with Express.js. Each engine has its own strengths and is suitable for different types of projects.

If you're interested in learning more about EJS templates specifically, consider exploring advanced features like partials, layouts, and filters. These features can help you build more complex and maintainable web applications.

Happy coding!


PreviousError Handling in Express.jsNext EJS (Embedded JavaScript) Templates

Recommended Gear

Error Handling in Express.jsEJS (Embedded JavaScript) Templates