The Pug Guide: A Comprehensive Introduction to the Pug Templating Engine
tags: [“Pug”, “template engine”, “Express”, “Node.js”, “Jade”]
Introduction to Pug
Pug is a template engine designed for server-side Node.js applications. It allows developers to add dynamic data to views and generate HTML. Formerly known as Jade 2.0, the project changed its name to Pug in 2016 due to a trademark issue. While the older version (Jade 1.0) is still usable, it is recommended to use the latest version (Pug 2.0). Learn more about the differences between Jade and Pug.
How does Pug look?
In Pug, tags and their content are specified differently compared to other template engines. For example, the following Pug code creates a <p>
tag with the content “Hello from Flavio”:
1 | p Hello from Flavio |
Install Pug
Installing Pug is as simple as running the following command:
1 | npm install pug |
Setup Pug to be the template engine in Express
To use Pug as the template engine in an Express application, you need to set it up in the following way:
1 | const path = require('path') |
Your first Pug template
Here’s an example of creating an “about” route that renders a Pug template named “about.pug”:
1 | app.get('/about', (req, res) => { |
The “about.pug” template contains the following code, which creates a <p>
tag with the content “Hello from Flavio”:
1 | p Hello from Flavio |
Interpolating variables in Pug
You can interpolate variables in Pug templates by passing them as an object during rendering. For example:
1 | app.get('/about', (req, res) => { |
And in the Pug template, you can interpolate the value of the “name” variable using the #{}
syntax:
1 | p Hello from #{name} |
Interpolate a function return value
To interpolate the return value of a function in Pug, pass the function in the rendering step and use the #{}
syntax to interpolate its result:
1 | app.get('/about', (req, res) => { |
1 | p Hello from #{getName()} |
Adding id and class attributes to elements
To add id
and class
attributes to elements, use the following syntax in Pug:
1 | p#title |
Set the doctype
To specify the HTML doctype in Pug, use the following syntax:
1 | doctype html |
Meta tags
To add meta tags to your Pug template, use the following syntax:
1 | html |
Adding scripts and styles
To include external scripts and stylesheets in your Pug template, use the following syntax:
1 | html |
Inline scripts
To include inline scripts in your Pug template, use the following syntax:
1 | script alert('test') |
Loops
To iterate over an array in Pug, use the each
keyword:
1 | ul |
Conditionals
To use conditionals in Pug, use the if
, else if
, and else
keywords:
1 | if name |
Set variables
You can set variables in Pug templates using the - var
syntax:
1 | - var name = 'Flavio' |
Incrementing variables
To increment a numeric variable in Pug, you can use the ++
operator:
1 | age++ |
Assigning variables to element values
To assign variables to element values in Pug, use the following syntax:
1 | p= name |
1 | span.age= age |
Iterating over variables
To iterate over variables in Pug, you can use either the for
or each
keyword:
1 | for dog in dogs |
1 | ul |
To get the number of items in an array, use the .length
property:
1 | p There are #{values.length} |
Including other Pug files
To include other Pug files in your template, use the include
keyword:
1 | include otherfile.pug |
Defining blocks
To define blocks in Pug, which can be overridden by extending templates, use the block
keyword:
1 | html |
Extending a base template
To extend a base template in Pug, use the extends
keyword and redefine the necessary blocks:
1 | extends home.pug |
Comments
Pug supports both visible and invisible comments. Visible comments are rendered in the resulting HTML, while invisible comments are not.
Visible comments
Inline:
1 | // some comment |
Block:
1 | // |
Invisible comments
Inline:
1 | //- some comment |
Block:
1 | //- |
Now that you have a comprehensive guide to using the Pug templating engine, you can start building dynamic and efficient websites with Node.js applications.