If you‘ve ever developed a website with multiple HTML pages, you know how tedious and error-prone it can be. You end up with a lot of duplicated code for common elements like the , header, and footer. Making a small change requires editing every single page. It‘s a hassle to keep everything consistent and in sync.
But there‘s a better way! By using a templating language like Pug, formerly known as Jade, you can greatly simplify the process of building multipage HTML sites. Pug makes it easy to reuse code, reduce duplication, and keep your project more organized and maintainable.
What is Pug?
Pug is an HTML templating language that compiles to HTML. It uses indentation rather than closing tags to define the structure of the document. This results in very concise and readable code. For example, here‘s a simple Pug template:
doctype html
html
head
title My Page
body
h1 Welcome
p Hello world!
Pug has a lot of powerful features like variables, includes, mixins, conditionals, and loops that make it really flexible. But perhaps the biggest benefit of Pug for multipage sites is the ability to extend a base layout template. This allows you to define the common structure and elements once, then override only the parts that change on each page.
Why Use Pug for HTML Development?
Here are some of the key benefits of using Pug to build HTML pages, especially multipage websites:
• Code reuse – Define common code in a layout that each page extends
• Less repetition – No more copying and pasting the same and tags everywhere
• Easier maintenance – Make a change in one place instead of on every page
• Cleaner, more readable code – Indentation-based structure is easier on the eyes
• HTML preprocessing – Use variables, conditionals, iteration in your HTML
• Partials and includes – Break up HTML into smaller, more modular and reusable chunks
• Mixins for UI components – Create custom tags for reusable UI elements
Setting Up a Pug Project
To use Pug, you‘ll need to set up a Node.js project and install a few dependencies. First, create a new directory for your project and run npm init inside it to generate a package.json file. Then install these dependencies:
This will install Pug itself, the Pug loader for Webpack, Webpack to compile the Pug templates, and the HTML Webpack plugin to generate HTML files from the compiled templates.
Next, set up a src directory for your Pug templates and a webpack.config.js file to configure Webpack. Here‘s an example folder structure:
The key to code reuse in multipage Pug sites is the layout template. This defines the common structure and elements that will be shared by all pages. Here‘s an example layout.pug file:
doctype html
html
head
include includes/head.pug
body
include includes/header.pug
block content
include includes/footer.pug
This layout does a few important things:
It defines the overall document structure with the doctype, html, head, and body tags.
It includes a head.pug partial file to contain the contents of the <head> tag, which will be shared by all pages.
It includes a header.pug partial for the <header> that appears at the top of every page.
It defines a block called "content" where page-specific content will be inserted.
It includes a footer.pug partial for the common <footer>.
The block is the most important part. This serves as a placeholder that can be overridden by each individual page template.
Creating Page Templates
With the layout.pug in place, you can now create a separate .pug file for each page on your site. Here‘s an example index.pug page template:
extends layout.pug
block content
h1 Welcome to my site
p This is some page-specific content
ul
li Item A
li Item B
The key things here are:
The extends declaration tells Pug that this template should inherit from layout.pug.
The block content fills in the content block defined in the layout with the page-specific HTML.
This approach makes the page template extremely minimal and readable, containing only the unique content for that page. The common code is all contained in the layout.pug and various partials.
To create additional pages, just add a new .pug file for each one, extending layout.pug and defining its unique content. For example, about.pug might contain:
extends layout.pug
block content
h1 About Us
p We are a small but mighty team of developers...
Using Includes for Partials
In addition to the layout, you can use Pug‘s include feature to break up your code into smaller, more modular chunks known as partials. A common example is a navigation menu. Rather than copying and pasting the
Webpack is a powerful and flexible tool for bundling and optimizing your front-end assets. It can handle not only JavaScript, but also CSS, images, fonts, and more. Webpack also allows you to easily split your code into multiple bundles for better performance and caching. Webpack can seem intimidating at first with its huge array of…
As web applications become increasingly complex, developers need powerful tools to optimize their code and streamline development. One such tool that has taken the web development world by storm is webpack. Webpack is a static module bundler for modern JavaScript applications. At its core, webpack is a tool that takes your web application‘s dependencies (JavaScript…
As a full-stack developer, you understand the importance of writing clean, maintainable code. One way to achieve this is by sharing variables across your front-end codebase. Webpack, a powerful module bundler, makes it easy to define variables in one place and inject them into your HTML templates, CSS styles, and JavaScript modules. In this in-depth…
As a React developer, you‘ve likely heard of Webpack but may not be sure exactly what it does or how to set it up. In this in-depth tutorial, we‘ll walk through configuring Webpack in a React project from scratch, adding some commonly used features, and discuss best practices to help you get the most out…