What is pug?
Pug is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It provides a clean and easy-to-read syntax that helps in writing HTML templates. Pug templates are compiled into HTML and can be rendered in the browser or on the server.
What are pug's main functionalities?
HTML Generation
Pug allows you to write simplified syntax that compiles into HTML. The above code will generate a standard HTML5 document structure with a title and a heading.
doctype html
html(lang='en')
head
title My Site
body
h1 Welcome to My Site
Dynamic Content
You can use variables in Pug to dynamically insert content into your templates. The above code will output an h1 tag with the user's name.
- var user = { name: 'John Doe' }
h1= user.name
Conditionals
Pug supports conditional statements, allowing you to render different parts of your template based on certain conditions. In this example, the h1 tag will only be rendered if the user is logged in.
- var user = { loggedIn: true }
if user.loggedIn
h1 Welcome back!
Loops
Pug provides a way to iterate over arrays and objects with loops. This code will produce an unordered list with each fruit as a list item.
- var items = ['Apple', 'Banana', 'Cherry']
ul
each item in items
li= item
Mixins
Mixins in Pug are reusable blocks of Pug that can be included anywhere in your templates. The above mixin creates a hyperlink, and it is used to add a link to Google.
mixin link(href, name)
a(href=href)= name
+link('http://google.com', 'Google')
Includes and Extends
Pug allows you to include content from other Pug files and extend templates to create a base template (layout) that can be shared across multiple pages. The 'index.pug' extends 'layout.pug' and provides content for the title and body blocks.
// layout.pug
doctype html
html
head
block title
body
block content
// index.pug
extends layout.pug
block title
title My Page
block content
h1 My Page Content
Other packages similar to pug
handlebars
Handlebars is a popular templating engine that uses a more Mustache-like syntax. It is known for its simplicity and ease of use. Unlike Pug, which uses indentation to define structure, Handlebars uses double curly braces for expressions and does not rely on whitespace.
ejs
EJS, or Embedded JavaScript templates, is another alternative to Pug. It embeds raw JavaScript code within templates and uses angle brackets and percent signs to delimit JavaScript code. EJS syntax is more similar to traditional HTML, which might be easier for beginners.
mustache
Mustache is a logic-less template syntax that can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. Mustache is simpler than Pug and does not support features like loops and conditionals directly within the template.
nunjucks
Nunjucks is inspired by Jinja2 and is a powerful templating engine with block inheritance, auto-escaping, macros, asynchronous control, and more. It has a syntax more verbose than Pug's and can be more familiar to those who have worked with HTML before.