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

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

Pug (formerly Jade) Templates

Updated 2026-05-15
10 min read

Pug (formerly Jade) Templates

Introduction

In the world of web development, templating engines play a crucial role in separating presentation logic from business logic. One such popular templating engine is Pug (formerly known as Jade), which offers a clean and concise syntax for writing HTML templates. This tutorial will introduce you to Pug's basic concepts, syntax, and how it can be integrated into your web projects.

Concept

Pug is a high-performance template engine that compiles into optimized JavaScript code. It allows developers to write HTML in a more readable and maintainable way by using indentation-based syntax instead of traditional HTML tags. This approach reduces the amount of boilerplate code and makes templates easier to manage, especially for larger projects.

Key Features of Pug

  1. Indentation-Based Syntax: Unlike HTML, which uses closing tags, Pug uses indentation to define the structure of the document.
  2. Mixins: Reusable components that can be defined and used throughout your templates.
  3. Filters: Allows you to process template content with external tools or libraries.
  4. Inheritance: Enables template inheritance, making it easier to create consistent layouts across multiple pages.

Examples

Let's dive into some practical examples to understand how Pug works.

Basic Syntax

Here is a simple example of how to write HTML using Pug:

pug
1doctype html
2html(lang="en")
3head
4 title My Page
5body
6 h1 Hello, World!

When compiled, this Pug code will produce the following HTML:

Output
<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Page</title>
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>

Attributes and Classes

You can add attributes to elements using parentheses or curly braces. Here's how you can define classes and other attributes:

pug
1doctype html
2html(lang="en")
3head
4 title My Page
5body(class="container")
6 h1(class="title") Hello, World!

This will compile to:

Output
<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Page</title>
</head>
<body class="container">
  <h1 class="title">Hello, World!</h1>
</body>
</html>

Mixins

Mixins are reusable components that can be defined and used throughout your templates. Here's an example of how to create and use a mixin:

pug
1doctype html
2html(lang="en")
3head
4 title My Page
5body(class="container")
6 mixin button(text)
7 button(type="button")= text
8
9 +button('Click Me')

This will compile to:

Output
<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Page</title>
</head>
<body class="container">
  <button type="button">Click Me</button>
</body>
</html>

Inheritance

Pug supports template inheritance, which is useful for creating consistent layouts. Here's an example:

layout.pug

pug
1doctype html
2html(lang="en")
3head
4 title My Page
5body(class="container")
6 block content

index.pug

pug
1extends layout
2
3block content
4h1 Hello, World!
5p This is a paragraph.

When index.pug is compiled, it will extend the layout.pug and replace the content block:

Output
<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Page</title>
</head>
<body class="container">
  <h1>Hello, World!</h1>
  <p>This is a paragraph.</p>
</body>
</html>

What's Next?

Now that you have a good understanding of Pug and its syntax, you might want to explore other templating engines like Handlebars Templates. Handlebars offers a different approach with its own set of features and capabilities, which can be useful depending on your project requirements.

Feel free to experiment with Pug in your next web development project and see how it can simplify your HTML templates.


PreviousEJS (Embedded JavaScript) TemplatesNext Handlebars Templates

Recommended Gear

EJS (Embedded JavaScript) TemplatesHandlebars Templates