What is Pug?

Pug (formerly known as Jade) is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers.

Key Features of Pug

  • Clean and readable syntax - no need for closing tags or angle brackets
  • Whitespace sensitivity - indentation defines nesting structure
  • HTML shorthand - use class and ID shortcuts
  • Template inheritance - reuse and extend templates
  • Mixins - reusable blocks of code
  • JavaScript integration - embed JavaScript logic directly in templates

Example Comparison

HTML
<div class="container">
  <h1>Hello World</h1>
  <p>This is a paragraph</p>
  <ul class="list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
  </ul>
</div>
Pug
.container
  h1 Hello World
  p This is a paragraph
  ul.list
    li Item 1
    li Item 2
    li Item 3

Why Use Pug?

Improved Productivity

Pug's concise syntax means less typing and more doing. The clean and readable code structure leads to fewer errors and better maintainability.

Powerful Templating

Pug supports template inheritance, includes, mixins, and other features that make it perfect for building modular web applications.

JavaScript Integration

Embed JavaScript code directly in your templates. Conditionals, loops, and variable interpolation are seamless with Pug.

Framework Compatibility

Pug works great with Express, Koa, and other Node.js frameworks. It's also compatible with build tools like Webpack.