Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2854

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T09:18:09+00:00 2024-11-26T09:18:09+00:00

Pug: A HTML Templating Language

  • 61k

This series builds on my post, Supercharge Your Web Dev Workflow With Emmet, to which Maciek Fitzner posted a comment mentioning Pug.

Templating languages are widely used in Web development and two of the most popular ones are Pug and Slim. In this series, we're going to learn the basics of these two and hopefully they would help improve your workflow further.

Table of Contents

  • Emmet vs. Templating Languages
  • Meet Pug
    • An Overview
    • How It Works
  • Start a Pug Project
  • Next Steps

Emmet vs. Templating Languages

The fundamental difference between Emmet and templating languages like Pug and Slim is that,

  • With the former which is just an editor extension, we write abbreviations which expand to full HTML code instantly when we press TAB. So we're basically still writing in HTML.
  • The latter, however, are different languages than HTML and have their own syntax. This means these template files must be converted (compiled, as we call it in the programming world) to HTML before being forwarded to browsers to render.

It should be noted that many templating languages do not only support HTML, but can generate other types of files as well. In this series, however, we'll focus on HTML.


Meet Pug

An Overview

Pug is a templating language for Node.js and the browser. It is often used to generate HTML, but it can also be used to generate XML or other types of documents.

How It Works

Here's an example of a simple Pug template that generates an HTML page with a heading and a paragraph:

  doctype html html   head     title My Site   body     h1 Hello, World!     p This is my site.   
Enter fullscreen mode Exit fullscreen mode

This template would be compiled into the following HTML:

  <!DOCTYPE html> <html>   <head>     <title>My Site</title>   </head>   <body>     <h1>Hello, World!</h1>     <p>This is my site.</p>   </body> </html>   
Enter fullscreen mode Exit fullscreen mode

The rendered page from the HTML looks like this:

Rendered page from sample

Rendered page from sample

As the example demonstrates, Pug uses a combination of whitespace, indentation, and special characters to distinguish between HTML elements, code blocks, and other constructs.

The Pug file is then compiled into HTML files before being served to the browser for rendering. In fact, all templating languages work in similar manner because browers can only read HTML.

How Pug works

How Pug works

Start a Pug Project

Before starting a pug project, make sure you have Node.js installed in your computer. Installation of Node.js is beyond this post, so do a bit of googling if you need any help.

Follow these steps to start a new Pug project:

Step 1:

Create a new directory for your project and navigate to it in the terminal. For example:

  mkdir ~/my-pug-project cd ~/my-pug-project   
Enter fullscreen mode Exit fullscreen mode

Step 2:

Initialize the project by creating a package.json file in your project root and install the Pug package. For example:

  npm init -y npm install pug   
Enter fullscreen mode Exit fullscreen mode

Or if you choose to use yarn:

  yarn init -y yarn add pug   
Enter fullscreen mode Exit fullscreen mode

Step 3:

Create a directory in your project root to hold your Pug templates and create a pug file in it:

  mkdir views touch views/index.pug   
Enter fullscreen mode Exit fullscreen mode

Then give the new file the following content:

  doctype html html   head     title My Site   body     - var name = 'Jesse'     h1 Hello, #{name}!     p       | This is my site. It is powered by       |       strong Pug       | .     - var users = ['Alice', 'Bob', 'Charlie', 'Dan']     ul       each user in users         li= user     if users.length > 3       p There are more than 3 users in the list.     else       p There are 3 or fewer users in the list.   
Enter fullscreen mode Exit fullscreen mode

In this pug file, we used some more advanced features of Pug such as variables, piped text, iteration and conditionals. If you want to further familiarize yourself with these concepts, check out the links.

Our pug template generates the following HTML code:

  <!DOCTYPE html> <html>   <head>     <title>My Site</title>   </head>   <body>     <h1>Hello, Jesse!</h1>     <p>This is my site. It is powered by       <strong>Pug</strong>.     </p>     <ul>       <li>Alice</li>       <li>Bob</li>       <li>Charlie</li>       <li>Dan</li>     </ul>     <p>There are more than 3 users in the list.</p>   </body> </html>   
Enter fullscreen mode Exit fullscreen mode

Step 4:

We need a server to compile our Pug file into HTML file, so at our project root, create a server.js file:

  touch server.js   
Enter fullscreen mode Exit fullscreen mode

And add the following content to it:

  const express = require('express'); const app = express();  // Set the view engine to pug app.set('view engine', 'pug');  // Set the views directory app.set('views', './views');  // Define a route for the home page app.get('/', (req, res) => {   res.render('index'); });  // Start the server const port = process.env.PORT || 3000; app.listen(port, () => {   console.log(`Listening on port ${port}...`); });   
Enter fullscreen mode Exit fullscreen mode

As we're using the express package here, we need to install it:

  npm install express   
Enter fullscreen mode Exit fullscreen mode

Or:

  yarn add express   
Enter fullscreen mode Exit fullscreen mode

If you need help with express or want to learn more about it, check out its official docs.

Step 5:

Start the server by running:

  node server.js   
Enter fullscreen mode Exit fullscreen mode

Now go to http://localhost:3000 and boom! Our page should be rendered properly like this:

Rendered page from advanced example

Rendered page from advanced example

Next Steps

In the first part of the series, we learned the basics of Pug, a popular templating language for generating HTML. We saw some simple but inspiring examples that should be enough to help you get started.

If you want to dive a bit deeper into Pug, I'd recommend you have a look at their docs. Each of the topics is explained with code examples that you can try out instantly.

In the next part, I'll walk you through another templating language, Slim. So please follow me to stay tuned.

htmlpugtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.