Make Multipage HTML Development Suck Less with Pug

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:

npm install --save-dev pug pug-loader webpack webpack-cli html-webpack-plugin

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:

src/
  includes/
    head.pug
    header.pug
    footer.pug
  pages/
    index.pug
    about.pug
    contact.pug
  layout.pug
webpack.config.js
package.json

Creating a Layout Template

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:

  1. It defines the overall document structure with the doctype, html, head, and body tags.
  2. It includes a head.pug partial file to contain the contents of the <head> tag, which will be shared by all pages.
  3. It includes a header.pug partial for the <header> that appears at the top of every page.
  4. It defines a block called "content" where page-specific content will be inserted.
  5. 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:

  1. The extends declaration tells Pug that this template should inherit from layout.pug.
  2. 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

Similar Posts