As with the SASS preprocessor, the Pug uses an interpreter, a program, or a script that translates a Pug template into HTML.
This course will use the npm package
pug-cli, allowing you to compile files from the command line. It is similar to using the
sass package from the SASS: Fundamentals course.
pug-cli package to install the Pug interpreter:
npm install pug-cli
You can install the package in a specific directory or globally with the
-g flag during installation. If you want to know the package version you installed, type
At the time of writing, the following version is in use:
pug version: 2.0.4 pug-cli version: 1.0.0-alpha6
The first template
Let us look at how the interpreter works. To do this, create an index.pug file with the following contents:
doctype html html(lang='en') head meta(charset='UTF-8') meta(name='viewport' content='width=device-width, initial-scale=1.0') link(rel='icon' type='image/x-icon' href='favicon.ico') title My first template using Pug. link(rel='stylesheet' href='./styles/app.css') body h1 Pug template p | My first template using Pug. | It'll make it easier for me to design layouts for pages.
Even if this is the first time you see Pug markup, you will have no trouble figuring out what we do here. It lowers the entry threshold for people familiar with HTML syntax.
Now it is time to compile this code. To do that, we enter the command
pug and pass the path to the file you want to compile.
If we enter no other options, the file will be compiled automatically under the same name and in the same directory:
pug index.pug --pretty
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <title>My first Pug template</title> <link rel="stylesheet" href="./styles/app.css"> </head> <body> <h1>Pug template</h1> <p> My first template using Pug. It'll make it easier for me to design layouts for pages. </p> </body> </html>
Pug translates the code into minimized HTML by default. It removes the spaces between tags, tabs, and line feeds. It is handy in development but not in learning. During studying, you better see the complete and unadulterated result. Luckily, we have the
--pretty option for this. If you are sure about your code, remove it.
In development, compiled files are often kept separate from source files. It is convenient because it means we develop in one directory, and the files that result from compilation, which will go to the server, are in another directory. In
pug-cli, we use the
-o for this purpose, giving the path where we send the compiled file:
pug index.pug --pretty -o ./build/
When developing, compiling files after each save is very convenient. We use the
-w flag for this. After that, the process will constantly monitor the file and compile whenever there are changes. Altogether, your learning projects may look like the following:
pug index.pug --pretty -w -o ./build/
If these flags are too complex initially, you can use their full names:
pug index.pug --pretty --watch --out ./build/
A complete command list can be displayed using the
pug --help command.
- Install the npm package
- Compile the file from the example
- Try using different tags
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.