There’s no right or wrong answer to this question, but in light of Vue, React, Angular, and other languages, is it even worth it to learn Pug or other templating languages?
I know Vue uses the mustache syntax {{variable}}. I presume React and Angular do something similar.
It’s likely that I’ll learn it for the class, but I’m not sure if I should keep studying on my own time.
Yes, Pug is still a good choice, and it’s also a good way to learn how to use data.
You’re right that it’s not the same as the others you mentioned, but that depends on the project you’re working on.
For example, my team uses pug with hexo for a static site that populates an internal styleguide for a front end framework. We use pug because it is very simple to use and doesn’t require much knowledge of languages other than native JavaScript.
But Angular is also used by my team for some web apps because the tools for it work better with the project.
Because of this, whether I tell you to use it or not depends on your project needs, goals, and interests.
I think you should learn it, but don’t spend too much time on it. As I already said, pug is a lot like native JavaScript, so once you have a basic understanding, move on to learning more complex frameworks like Angular, React, etc.
At the time of this post (Jun 15, 2020) handlebars.js is the most popular according to npmtrends.com
Mastering Pug Template Engine Top Interview Questions and Answers
Pug (formerly known as Jade) has become one of the most popular templating engines for Node.js due to its clean and easy-to-read syntax. As a developer having a strong command over Pug can help you stand out during technical interviews. In this comprehensive guide, I will provide some of the most frequently asked Pug interview questions that test your knowledge of Pug’s core concepts advanced features, integration with Node.js frameworks, and real-world applications.
Let’s get started!
What is Pug and How Does it Work?
Pug is a high-performance template engine that compiles Pug templates into HTML. It uses indentation rather than closing tags to represent nested elements. This results in cleaner and more readable code compared to plain HTML.
Under the hood, Pug maintains a tree representation of the HTML structure. When a template is compiled, this abstract syntax tree is transformed into corresponding HTML markup which can be served to the client. The compilation step allows Pug to optimize and validate the output before generating HTML.
Some key advantages of using Pug over plain HTML include:
- More concise and cleaner syntax
- Support for variables, mixins and reusable blocks
- Client-side compilation for templating on browser
- Integration with Node.js frameworks like Express
Core Concepts in Pug
Q1. What is the difference between Pug and Jade?
Originally called Jade, the project was later renamed to Pug due to trademark issues. Pug and Jade are essentially the same templating engines – the underlying technology remains unchanged. The transition from Jade to Pug was purely due to naming conflicts.
Q2. How does Pug simplify writing HTML?
Pug minimizes the boilerplate in HTML through:
- Indentation-based nesting for representing structure
- No closing tags required
- Shorthand syntax for ids, classes, attributes
- Mixins for reusing code blocks
- Extends for inheritance
- Includes for break up code into smaller files
This templating syntax results in more compact and cleaner templates compared to plain HTML.
Q3. What are some key features of Pug?
Some notable features of Pug include:
- Whitespace sensitive syntax
- Dynamic code generation using JavaScript
- Template inheritance and mixins
- Native support for Express engine
- Client-side browser support
- Live compilation for development
- Robust API for customization
- Active open-source community
Q4. How does Pug simplify writing nested elements?
Pug relies on indentation rather than closing tags to represent nested element structures. Child elements are indented from their parent elements. This convention eliminates the need for closing tags and gives Pug templates a clean, nested appearance.
For example:
div h1 Title p Content
The <p>
tag is nested inside <div>
based on indentation rather than close tags.
Q5. Can you explain the difference between buffered and unbuffered code in Pug?
In Pug, buffered code outputs directly into the rendered HTML. Unbuffered code runs but does not output to HTML.
For example:
- var username = "John" // unbuffered, does not outputp= "Hello " + username // buffered, outputs "Hello John"
Buffered code starts with =
whereas unbuffered code starts with -
. Buffered code escapes characters by default while unbuffered code does not escape.
Pug Syntax and Features
Q6. How do you write comments in Pug?
Pug supports both single line and multi-line comments:
// This is a single line comment /* This is a multi-line block comment.*/
We can also combine //
and /* */
syntax:
// This is a multi-line comment using //
Comments are useful for leaving notes and documentation within Pug templates.
Q7. How do you output plain text in Pug?
We can output plain text using the |
character. For multi-line text blocks, use a .
after |
:
p | This text will | output | as-isp. This is a multi-line plain text block
The .
after |
informs Pug that this is a text block that retains all whitespace.
Q8. How are variables interpolated in Pug?
Pug variables are interpolated using the #{ }
syntax. For example:
- var name = "John"p Hello #{name}
This outputs <p>Hello John</p>
. JavaScript expressions can also be embedded within #{ }
.
Q9. How do you conditionally output content in Pug?
We can use if
, else if
, and else
to conditionally render content. For example:
- var user = { admin: true }if user.admin p You are an adminelse p You are not an admin
Conditional statements must always start on a new line beginning with -
. The conditional blocks are nested based on indentation.
Q10. How do you iterate over arrays and objects in Pug?
Pug provides an each
construct to iterate over arrays and objects:
- var users = ["John", "Jane"]each user in users li= user
This will output a <li>
for each user in the array. We can also iterate over keys and values of an object:
- var user = {name: "John", age: 20}each val, key in user p #{key} : #{val}
The iteration syntax makes it easy to build lists dynamically.
Q11. How are mixins useful in Pug?
Mixins allow you to define reusable blocks of Pug code that can be included in multiple templates. For example:
mixin list ul li Item 1 li Item 2 +list+list
This list
mixin can now be reused throughout the templates. Mixins are useful for header, footer, widgets and repeated UI patterns.
Integrating Pug with Node.js
Q12. How do you configure Pug as the template engine in an Express app?
To use Pug templates in Express:
-
Install Pug:
npm install pug
-
Require Express and Pug modules
-
Set the view engine to use Pug:
app.set('view engine', 'pug')
- Set the views directory:
app.set('views', path.join(__dirname, 'views'))
- Render Pug templates using
res.render()
:
res.render('index')
This will render index.pug
in the views folder.
Q13. How does Pug integrate with Node.js frameworks like Express?
Pug provides out-of-the-box integration with Express and other Node.js frameworks via npm packages. Some key advantages are:
- The
express-pug
middleware enables Pug rendering - Pug is set as the default view engine in Express apps
- The
res.render()
method can be used to render Pug templates - Pug files can be passed localized variables from routes
- Live reloading available during development
- Caching and localization support
The tight integration makes Pug a natural choice for use with Node.js web frameworks.
Q14. Can you share an example using Pug with Express?
Here is a simple Pug app with Express:
index.js:
const express = require('express')const app = express()app.set('view engine', 'pug')app.get('/', (req, res) => { res.render('index', {title: 'Home Page'}) })app.listen(3000)
views/index.pug:
h1= titlep Welcome to my app
This renders index.pug
, passing it a title
variable. The variable is interpolated using #{title}
.
Q15. How do you serve static assets like CSS and images in Pug?
The express.static
middleware can be used to serve static assets. For example:
app.use(express.static('public'))
This will serve assets from the public
folder. In Pug templates, reference them using root relative path:
link(rel='stylesheet', href='/styles.css') img(src='/logo.png')
The assets will be served from the public
folder while retaining the root relative URLs in Pug files.
Advanced Pug Questions
Q16. Can you explain template inheritance in Pug?
Template
Welcome to the Treehouse Community
Need help with code errors, bugs, or just want another set of eyes on your latest project? Join the Treehouse Community and get help from other developers, designers, and programmers of all skill levels and backgrounds. While you’re here, check out some resources that other Treehouse students have shared.
Pug Template Engine – Full Tutorial for Beginners
FAQ
Why do we use PUG template engine?
What is the syntax of PUG?
What is PUG used for?
What is the difference between PUG and react?
Why use Pug for template engine?
Pug is a compiling template engine that throws build errors during the compilation of the .pug file to plain HTML if the indentation isn’t correct. This makes it an error prevention tool for Front-end development. Now that I have your attention, let’s start using it:
What is Pug in JavaScript?
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 how to use pug and its syntax examples. What Is Pug Used For?
Is Pug a good JavaScript template engine?
Pug is a good JavaScript template engine. I’d encourage you to check out its excellent documentation and to just start using it in your projects. You can also use it with several modern JS frameworks, such as React or Vue, and it has even been ported to several other languages.
How do I compile a Pug template to HTML?
To compile a Pug template to HTML, open the terminal, navigate to the folder containing the Pug file, and run the command: ‘pug index.pug -o index.html’. This simple command will create the corresponding HTML file next to the Pug one.