Elements in Pug View Engine
Last Updated :
27 Mar, 2024
Pug is a template engine that works with JavaScript libraries and frameworks. It simplifies the process of writing HTML by reducing traditional HTML syntax. It uses indentation to represent HTML structure. By using Pug you can reuse the HTML code.
In this article, we will learn about Pug Elements with its syntax and example.
Pug Elements:
Pug elements are used to define the HTML structure in Pug templates. It represents various HTML tags used to define the layout and content of a web page. Pug elements are constructed using indentation-based syntax that makes the code visually clean and easy to read.
Syntax:
The syntax for defining Pug elements is straightforward. Each element is represented by its tag name followed by optional attributes, and content and arranged hierarchically using indentation.
// This is a Pug template demonstrating basic syntax with comments
// Define document type and language
doctype html
html(lang="en")
// Head section containing metadata and stylesheets
head
title My Pug Page
meta(charset="UTF-8")
link(rel="stylesheet", href="styles.css")
// Internal CSS
style.
h1 {
color:green;
}
// Body of the document
body
// Header section with an ID
header#main-header
// Element with class and text content
h1.heading Welcome to My Website
// Navigation menu with list items
nav.menu
ul
// List item with class and href attribute
li.menu-item
a(href="#") Home
li.menu-item
a(href="#") About
li.menu-item
a(href="#") Contact
// Main content section
section#content
// Container with ID and class
.container
// Paragraph with class and text content
p.intro This is a demonstration of Pug syntax.
// Image with src and alt attributes
img.image(src="image.jpg", alt="Example Image")
// Footer section
footer#main-footer
// Container with ID
.container
// Paragraph with text content
p Copyright © 2024 My Website. All rights reserved.
Features of Pug Elements:
- It supports variable, conditionals and loops directly in the template to generate dynamic content.
- It can be easily used with NodeJS frameworks like ExpressJS.
- It uses Indentation to define the structure of HTML.
- It reduces the traditional HTML syntax.
- It supports reusable components through mixins.
- It supports use of inline JavaScript.
Steps to Create NodeJS App and Installing Module:
Step 1: Create a NodeJS application using the following command:
npm init
Step 2: Install required Dependencies:
npm i pug express
The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.18.2",
"pug": "^3.0.2"
}
Example: The Below example is demonstrating the use of Pug Elements:
HTML
//File path: views/index.pug
html
head
// Set the title of the HTML page
title Pug Example
// Internal CSS styles for the page
style.
h1 {
color: green;
}
.heading {
font-style: italic;
}
body
// Display the main heading of the page
h1 GeeksForGeeks | Pug Elements
// Display a subheading
h4.heading This h4 with heading class
// Display a Ordered List
ol
li first
li second
li third
// Input Box
input(
type='text'
name='name'
placeholder='Enter Name'
)
button(type='submit') Submit
JavaScript
//File path: /index.js (root)
// Import required modules
const express = require('express');
const path = require('path');
// Create an Express application
const app = express();
// Define the port for the server to listen on
const port = 3000;
// Set Pug as the view engine
app.set('view engine', 'pug');
// Set the views directory to 'views' in the current directory
app.set('views', path.join(__dirname, 'views'));
// Define a route to render the Pug template
// when the root path is accessed
app.get('/', (req, res) => {
// Render the Pug template named 'index'
res.render('index');
});
// Start the server and listen on the specified port
app.listen(port, () => {
// Display a message when the server starts successfully
console.log(`Server is running at http://localhost:${port}`);
});
To run the application use the following command:
node index.js
Output: Now go to http://localhost:3000 in your browser:

Similar Reads
Comments in Pug View Engine Pug is a template engine for NodeJS and browsers to render dynamic reusable content. At compile time, the template engine compiles our Pug template code to HTML. Pug has many powerful features like conditions, loops, includes, etc. using which we can render HTML code based on user input or reference
3 min read
Conditionals in Pug View Engine Pug supports JavaScript natively, hence using JavaScript expressions, we can format HTML code. This approach allows us to reuse static web pages which have dynamic data. Angular brackets are not used while specifying the tags. Conditional Statement in PugConditional statements allow you to execute s
3 min read
Doctype in Pug View Engine In web development, the term "doctype" stands for Document Type Declaration. It's a key part, of defining the document type and telling the browser how to use it. In Pug, a template engine used with NodeJS, knowing the doctype helps create good HTML codeÂ. Table of Content Syntax of multiple D
4 min read
Tags in Pug View Engine Tags play a crucial role in Pug templates, allowing developers to create structured and dynamic HTML content. Let's explore the concept of tags in Pug in detail. Table of Content What are Tags in Pug?Tag AttributesClass and ID AttributesSelf-Closing TagsBlock ExpansionDynamic Tag NamesSteps to Setup
4 min read
Pug View Engine - Iteration Pug is a template engine for NodeJS and browsers to render dynamic reusable content. At compile time, the template engine compiles our Pug template code to HTML. Pug has many powerful features like conditions, loops, includes, and mixins using which we can render HTML code based on user input or ref
4 min read