Pug

Introduction

Pug is an HTML template engine with indentation-based syntax, mixins, and inheritance.

Basic document

doctype html
html(lang='en')
  h1.class#id(name='hi')
    | This is some text, hello there,
    = name

  - javascript()

See: Doctype, Tags

Elements

div
  | Just a div

.search
  | A div, with class 'search'

h1 A heading with text

h1= page.title

div.class
div.class1.class2
h1.header

See: Tags

Attributes

input(type='text' name='q' autofocus)

- var auth = true
body(class=auth ? 'authed' : 'anon')

See: Attributes

Comments

// This comment will appear in the HTML

//- This is a silent comment

//-
  Nesting inside a comment creates
  a comment block

See: Comments

Iteration

ul
  each user in users
    li= user

See: each

Layouts

Page

//- page.pug
extends layout.pug

block title
  | hello

block content
  | hello

Layout

//- layout.pug
title
  block title
body
  block content

See: Inheritance

Includes (partials)

Include

include ./includes/head.pug

Filtered include

include:markdown article.md

See: Includes

Multiline text

Text block

p.
  This is text that doesn't need to
  be prefixed by pipes.

Script block

script.
  // It's great for raw
  // JavaScript and stuff
  alert('hello')

See: Block in a tag

Conditionals

if authenticated
  a(href='/logout') Sign out
else
  a(href='/login') Sign in

See: Conditionals

Mixins

Mixins

Definition

mixin list
  ul
    ···

Call

+list

Mixins create reusable code blocks.

See: Mixins

Mixin attributes

Definition

mixin pet(name)
  span.pet= name

Call

+pet('cat')

See: Mixin attributes

Mixin blocks

Definition

mixin article(title)
  article
    h2.title= title
    block

Call

+article('hello there')
  p Content goes here

See: Mixin blocks

0 Comments for this cheatsheet. Write yours!