Install Pug
1 | yarn add pug |
Overview
pug.compile() will compile the Pug source code into a JavaScript function that takes a data object(called locals) as an argument. Call that resultant function with your data, then it will return a string of HTML rendered with your data.
The compiled function can be re-used, and called with different sets of data.
1 | // - template.pug |
1 | const pug = require('pug') |
Pug also provides the pug.render() family of functions that combine compileing and rendering into one step. However, the template function will be re-compiled every time render is called, which might impact performance. Alternatively, you can use the cache option with render, which will automatically store the compiled function into an internal cache.
1 | const pug = require('pug') |
Use with Express
Pug fully integrates with Express, as a supported view engine.
Production Defaults
In Express, the environmental variables NODE_ENV is designed to inform the web application of the execution environment: whether it is in development or in production. Express and Pug automatically modify the defaults of a few options in production environment, to provide a better out-of-the-box experience for users.
Specifically, when process.env.NODE_ENV is set to 'production', and Pug is used with Express, the compileDebug option is false by default, while the cache option is true.
API Reference
All API mehtods accept the following set of options:
filename: string
The name of the file being compiled. Used in exceptions, and required for relative
includeandextend. Default toPugbasedir: string
The root directory of all absolute inclusion
doctype: string
If the
doctypeis not specified as part of the template, you can specify it here. It is sometimes useful to get self-closing tags and remove mirroring of boolean attributes.filters: object
Hash table of custom filters. Default to
undefinedself: boolean
Use a
selfnamespaces to hold the locals. It will speed up the compilation, but instead of writingvariableyou will have to writeself.variableto access to property of the locals object. Default tofalsedebug: boolean
If set to
true, the tokens and function body are logged to stdoutcompileDebug: boolean
If set to
true, the function source will be included in the compiled template for better error messages. It is enabled by default, unless used with express in production mode.globals: Array
Add a list of global names to make accessible in templates
cache: boolean
If set to
true, compiled functions are cached.filenamemust be set as the cache key. Only applies torenderfunctions. Default tofalseinlineRuntimeFunctions: boolean
Inline runtime functions instead of
require-ing them from a shared version. ForcompileClientfunctions, the default istrue(so that one does not have include the runtime). For all other compilation or rendering types, the default isfalsename: string
The name of the template function. Only applies to
compileClientfunction. Default totemplate.
Methods
pug.compile(source[, options])
Compile a Pug template to a function, which can be rendered multiple times with different locals.
source: string
the source pug template to compile
options: An option object
returns: function
A function to generate the HTML from an object containing locals
1
2
3
4var pug = require('pug')
var compiledTemplate = pug.compile('string of pug', options)
var html = compiledFunction(locals)pug.compileFile(path[, options])
Compile a Pug template from a file to a function, which can be rendered multiple times with different locals.
path: string
The path to a Pug file
options: An option object
returns: function
A function to generate HTML from an object containing locals
1
2
3var pug = require('pug')
var compiledTemplate = pug.compileFile('path to pug file', options)
var html = compiledTemplate(locals)pug.compileClient(source[, options])
Compile a Pug template to a string of JavaScript, which can be used client side along with Pug runtime.
source: string
The Pug template to compile
options: an option object
returns: string
A string of JavaScript representing a function
1
2
3var pug = require('pug')
var compiledTemplate = pug.compileClient('string of pug', options)
var html = compiledTemplate(locals)pug.render(source[, options[, callback]])
source: string
The source pug template to render
options: an options object
callback: function
Node.js-style callback receiving the rendered results. The callback is called synchronously.
returns: string
The resulting HTML String
pug.renderFile(path[, options[, callback]])
path: string
the path to the pug file to render
option: an options object
callback: function
Node.js-style callback receiving the rendered results. This callback is called synchronously.
returns: function
The resulting HTML stirng
Attributes
Tag attributes look similar to HTML(with optional commas), but their values are just regular JavaScript.
1 | a(href='google.com') Google => <a href="google.com">Google</a> |
Normal JavaScript expression works fine:
1 | var authenticated = true |
Multiline Attributes
1 | input( |
If your JavaScript runtime supports ES6 template strings, you can use that syntax for attributes.
1 | input(data-json=` |
Quoted Attributes
If your attribute name contains odd character that might interfere with JavaScript syntax, either quote it using "" or '', or use commas to separate different attributes.
1 | div(class='div-class', (click)='play()') |
Attribute Interpolation
Simply write the attribute in JavaScript:
1
2
3
4
5
6
7var url = 'pub-test.html'
a(href='/' + url) Link => <a href='/pub-test.html'>Link</a>
var btnType = 'info'
var btnSize = 'lg'
button(type='button' class=`btn btn-${btnType} btn-${btnSize}`)
=> <button type="button" class="btn btn-info btn-lg"><button>
Boolean Attributes
1 | input(type='checkbox' checked) => <input type="checkbox" checked="checked" /> |
Style Attributes
The style attribute can be a string, like any normal attribute, but it can be an object, which is handy when styles are generated by JavaScript:
1 | a(style={color: 'red', background: 'green'}) => <a style="color:red;background:green;"></a> |
Class Attributes
The class attribute can be a string, like any normal attribute, but is also can be an array of class names, which is handy when generated from JavaScript.
1 | var classes = ['foo', 'bar', 'baz'] |
It can also be an object which maps class names to true or false values. This is useful for applying conditional classes:
1 | var currentURL = '/about' |
Class Literal
Classes may be defined using a .classname syntax:
1 | a.button => <a class="button"></a> |
Since divs are such a common choice of tag, it is default if you omit the tag name:
1 | .content => <div class="content"></div> |
ID Literal
1 | a#main-link => <a id="main-link"></a> |
&attributes
Pronounced as and attributes, the &attributes syntax can be used to expode an object into attributes of an element:
1 | dev#foo(data-json="foo")&attributes({'data-foo': 'bar'}) => <div id="foo" data-json="foo" data-foo="bar" /> |
Case
The case statement is a shorthand for JavaScript’s switch, it takes the following form:
1 | var friends = 10 |
Case Fall Through
You can use fall through, just as you would in a JavaScript switch statement
1 | var friends = 0 |
The difference, however, is a fall through in JavaScript happens whenever a break statement is not explicitly included.
Code
Pug allows you to write inline JavaScript code in your templates. There are three types of code: Unbuffered, Buffered, and Unescaped Buffered.
Unbuffered Code
Unbuffered code starts with -. It does not directly add anything to the output
1 | - for (var x = 0; x < 3; x++) |
Pug also supports block unbuffered code:
1 | - |
Buffered Code
Buffered code start with =. It evaluates the JavaScript expression and outputs the result. For security, buffered code is first HTML excaped.
1 | p = 'This code is <escaped>!' |
it can also be written inline with attributes, and supports the full range of JavaScript expression:
1 | p= 'This code is' + ' <escaped>!' => <p>This code is >escaped>!</p> |
Unescaped Buffered Code
Unescaped buffered code starts with !=. It evaluates the JavaScript expression and outputs the result. Unescaped buffered code does not perform any escaping, so is unsafe for user input.
1 | p != 'This code is <strong>not</strong> escaped!' => <p>This code is <strong>not</strong> escaped!</p> |
Comments
Buffered comments look the same as single-line JavaScript comments. They act sort of like markup tags, producing HTML comments in the rendered page.
Like tags, buffered comments must appear on their own line:
1 | // just some paragraphs |
Pug also supports unbuffered comments, Simply ass a hyphen(-) to the start of the comment.
These are only for commenting on the Pug code itself, and do not appear in the rendered HTML.
1 | //- will not output within markup |
Block Comments
1 | body |
Conditionals
Pug’s first-class conditional syntax allows for optional parenthese.
1 | - var user = { description: 'foo bar baz '} |
Pug also provides the conditional unless which works like a negated if. The following are equivalent:
1 | unless user.isAnonymous |
Filters
Include
Include allow you to insert the contents of one Pug file into another.
1 | // index.pug |
If the path is absolute(e.g. include /root.pug), it is resolved by prepending options.basedir.
If no file extension is given, .pug is automatically appended to the file name.
Including Plain Text
Including non-Pug files simply includes their raw text.
1 | // index.pug |
Template Inheritance
Pug supports template inheritance. Template instance works via block and extends keyword.
In a template, a block is simply a ‘block’ of Pug that a child template may replace. This process is recursive.
Pug blocks can provide default content, if appropriate. Providing default content is purely optional, though. The example below defines block scripts, block content and block foot
1 | // layout.pug |
To extend this layout, create a new file and use the extends directive with a path to the parent template(if no file extension is given, .pug is automatically appended to the file name). Then define one or more blocks to override the parent block content.
1 | // page-a.pug |
1 | // pet.pug |
Block append/prepend
Pug allows you to replace, prepend and append blocks
Suppose you have default scripts in a head block that you wish to use on every page you might do this:
1 | // layout.pug |
Interpolation
String Interpolation, Escaped
Consider the placement of the following tempalte’s locals: title, author and theGreat
1 | - var title = 'On Dogs' |
This can be valid JavaScript expression, so you can do whatever feels good.
1 | - var msg = 'not my inside voice' |
String Interpolation, Unescaped
You don’t have to play it safe, you can buffer unescaped values into your templates too
1 | - var riskyBusiness = '<em>Some of the girts</em>' |
Keep in mind that buffering unescaped content into your templates can be mighty risky if that content comes fresh from your users.
Tag Interpolation
Interpolation works not only on JavaScript values, but on Pug as well, just use the tag interpolation syntax like so:
1 | p. |
Whitespace Control
The tag interpolation syntax is especially useful for inline tags, where whitespace before and after the tag is significant.
By default, however, Pug removes all spaces before and after tags.
Iteration
Pug supports two primary methods of iteration: each and while
each
1 | ul |
While
1 | - var n = 0 |
Mixins
Mixins allow you to create reusable blocks of Pug
1 | // Declaration |
1 | mixin pet(name) |
Mixin Attributes
Mixins also get an implicit attributes argument, which is taken from the attributes passed to the mixin
1 | mixin link(href, name) |
Note: The values in
attributesby default are already escaped, you should use!=to avoid escaping them a secondtime.
Rest Arguments
You can write mixins that takes an unknown number of arguments using the rest arguments syntax
1 | mixin list(id, ...items) |
Plain Text
Pug provides four ways of getting plain text
Inline in a Tag
The easiest way to add plain text is inline. The first term on the line is the tag itsel. Everything after the tag and one space will be the text content of that tag.
1 | p This is plain old <em>text</em> content. |
Literal HTML
Whole lines are also treated as plain text when they begin with a left angle bracket (<), which may occasionally be useful for writing literal HTML tags in places that could otherwise be inconvenient.
1 | <html> |
Piped Text
Another way to add plain text to templates is to prefix a line with a pipe character (|). This method is useful for mixing plain text with inline tag.
1 | p |
Block in a Tag
Often you might want large blocks of text wihtin a tag. To do this, just add a . right after the tag name, or after the closing parenthesis, if the tag has attributes.
1 | script. |
Tags
By default, text at the start of a line (or after only white space) represents an HTML tag. Indented tags are nested.
To save space, Pug provides an inline syntax for nested tags.
1 | a: img => <a><img /></a> |
self-closing tag
1 | foo/ => <foo /> |