- This topic is empty.
-
Topic
-
Pug (formerly known as Jade) is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It allows developers to write HTML code more succinctly and maintainably by using a clean, whitespace-sensitive syntax.
Features of Pug
- Concise Syntax: Pug eliminates the need for closing tags and reduces the need for repetitive coding, making the markup more readable.
- Whitespace Sensitivity: Pug uses indentation to define the structure of the HTML, similar to languages like Python.
- Mixins and Inheritance: Pug supports mixins (reusable pieces of templates) and template inheritance, which can make your code DRY (Don’t Repeat Yourself).
- Embedded JavaScript: You can embed JavaScript directly in your Pug templates, making it easy to insert dynamic content.
Basic Example
Here is a basic example of a Pug template and the resulting HTML:
Pug Template (example.pug)
doctype html
html(lang="en")
head
title My PugExample
body
h1 Welcome to Pug
pThis is an example of a Pug template.
a(href="https://www.example.com")Visit Example.com
Resulting HTML
html
<html lang="en">
<head>
<title>My Pug Example</title>
</head>
<body>
<h1>Welcome to Pug</h1>
<p>This is an example of a Pug template.</p>
<a href="https://www.example.com">Visit Example.com</a>
</body>
</html>
Installation and Usage
To use Pug with Node.js, you need to install it via npm (Node Package Manager):
shnpm install pug
You can then use Pug in your Node.js application to render templates. Here’s a basic example:
Node.js Application (app.js)
javascriptconst express = require('express');
const app = express();
const path = require('path');
// Set the view engine to Pug
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
// Route to render the Pug template
app.get('/', (req, res) =>{
res.render('index', { title: 'My Pug Example',message: 'Welcome to Pug' });
});// Start the server
app.listen(3000,() => {
console.log('Server is running on http://localhost:3000');
});
Pug Template (views/index.pug)
pugdoctype html
html(lang="en")
head
title= title
body
h1= message
pThis is an example of a Pug template.
a(href="https://www.example.com") Visit Example.com
When you run this Node.js application and visit
http://localhost:3000
, you will see the rendered HTML from the Pug template.Benefits of Using Pug
- Readability: Pug’s clean and minimal syntax makes it easier to read and understand, especially for larger templates.
- Maintainability: With features like mixins and template inheritance, Pug promotes code reuse and maintainability.
- Efficiency: Pug compiles to optimized JavaScript, making the rendering process efficient.
- Integration: Pug integrates seamlessly with various frameworks and tools, particularly with Express in Node.js.
Pug is a powerful template engine that simplifies writing and managing HTML by providing a more readable and maintainable syntax. Its integration with JavaScript and Node.js makes it a popular choice for developers.
- You must be logged in to reply to this topic.