Pug Templating: Understanding extends, includes, layout.pug, and block
Pug, a powerful and efficient templating language for Node.js, offers various features that make creating dynamic web pages a breeze. Two crucial concepts in Pug are extends and includes. Understanding these concepts, along with the role of layout.pug
and block, is vital for creating structured and reusable Pug templates.
What is extends?
The extends keyword in Pug is used to inherit the structure of a parent template. It allows you to define a base layout or template that can be extended by other individual pages. This promotes code reusability and consistency across your website.
How does extends work?
Imagine you have a layout.pug
file containing the basic structure of your website, including the header, navigation, and footer. You can then create individual pages that extend from this layout, inheriting its structure and adding specific content for each page.
Here's an example of how to use extends:
layout.pug:
doctype html
html
head
title #{title}
body
header
h1 #{title}
nav
ul
li a(href="/") Home
li a(href="/about") About
main
block content
footer
p © 2023 My Website
index.pug:
extends layout.pug
block content
p Welcome to my website!
In this example, index.pug
extends the layout.pug
file. It inherits the entire structure, including the header, navigation, and footer. The block content section in index.pug
replaces the corresponding block content section in layout.pug
, allowing you to insert unique content specific to the homepage.
What is includes?
Includes allows you to embed the contents of one Pug file within another. This is useful for reusing common elements like headers, footers, or navigation menus across different pages.
Here's an example of how to use includes:
header.pug:
header
h1 My Website
index.pug:
doctype html
html
head
title My Website
body
include header.pug
main
p Welcome to my website!
footer
p © 2023 My Website
In this example, the include statement inserts the contents of header.pug
into the index.pug
file. This way, you can easily reuse the header across different pages without repeating the same code.
Why use layout.pug?
layout.pug
is typically used as a base template to define the overall structure of your website. By extending from layout.pug
, you can ensure consistent styling and structure across your pages.
What is block?
The block keyword in Pug allows you to define placeholder sections within a template that can be replaced by content from extending templates.
For example:
block content
p Default content
This block can then be replaced with specific content from the extending template.
Combining extends, includes, and block
You can combine extends, includes, and block to create complex and structured Pug templates. For instance, you can use extends to inherit a base layout, include reusable components like headers and footers, and use block to insert dynamic content specific to each page.
Best Practices
- Keep your
layout.pug
clean and organized: Use includes for reusable components like headers, footers, and navigation menus to avoid clutter. - Define clear and descriptive block names: This makes it easier to understand which sections of the layout are being replaced by extending templates.
- Use a consistent naming convention: This helps to maintain organization and clarity in your Pug templates.
In Conclusion
Understanding extends, includes, layout.pug
, and block in Pug is essential for creating dynamic and structured web pages. By utilizing these features, you can create reusable templates that promote code consistency and efficiency.