Pug Layout Inheritance: extends and block
Pug’s extends and block directives let you define a reusable layout once, eliminating duplicated markup and keeping templates DRY.
14 Feb 2026, 21:56 UTC

Problem: Duplicated Layout Markup
Many web projects repeat the same HTML skeleton—doctype, head, header, footer—in every template. This duplication makes global updates error-prone and bloats file size.
Thesis: Pug’s Inheritance System
Pug provides a declarative way to define a base layout once and reuse it across all views through the extends and block directives.
How extends and block Work
In a base layout file, block content acts as a placeholder. Child templates extend that layout and block content with page-specific markup. The inherited structure wraps the filled block, so only the unique content changes per view.
- Create a layout file with a
block contentsection. - Create a child view that
extendsthe layout and definesblock content. - Compile the child file and inspect the generated HTML to confirm the layout applies.
Example Files
layout.pug
doctype html
html
head
title My Site
body
header Nav
block content
footer © 2024
home.pug
extends layout.pug
block content
h1 Welcome
p This is the home page.
When home.pug is compiled, the output includes the doctype, html, head, header, and footer from layout.pug, with the block content replaced by the markup inside home.pug.
Using Inheritance with Express
In an Express application, Pug can be set as the view engine. A layout file renders once, and each route renders a child template that extends the layout. This reduces rendering overhead and simplifies asset management because the common HTML structure is generated a single time.
Trade-offs and Limitations
- Path validation: Using dynamic template names with
extendscan expose template files to injection if user input is passed directly to the path. Always validate and sanitize template references. - Relative path resolution: File paths are relative to the rendering file. Mis-configured paths can lead to silent failures or missing content. Double-check the location of layout files relative to child views.
- Engine mixing: When combining Pug with other templating engines in the same project, ensure layout files are not accidentally overridden or duplicated.
- Inheritance depth: Deeply nested inheritance chains can increase compilation time. Keep the depth of nested layouts reasonable.
Verification Steps
- Create a base layout file
layout.pugcontaining ablock contentsection and a header/footer. - Create a child view
home.pugthatextends layout.pugand defines theblock contentwith page-specific markup. - Compile the child file with the
pugCLI (pug home.pug -o output) and inspect the generated HTML to confirm the layout is applied. - Set up a minimal Express route rendering
home.pugand verify the response in a browser to ensure the layout renders correctly. - Optionally, use the online Pug REPL to paste both files and observe the rendered output instantly.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.