Tag attributes look similar to HTML (with optional commas), but their values are just regular
JavaScript.
(NOTE: Examples on this page use the pipe character (|) for whitespace control.)
a(href='//google.com') Google
|
|
a(class='button' href='//google.com') Google
|
|
a(class='button', href='//google.com') Google
Normal JavaScript expressions work fine, too:
- var authenticated = true
body(class=authenticated ? 'authed' : 'anon')
(body class="authed">(/body>
Multiline Attributes ¶
If you have many attributes, you can also spread them across many lines:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
If your JavaScript runtime supports ES2015 template strings (including Node.js/io.js 1.0.0 and
later), you can use that syntax for attributes. This is really useful for attributes with really
long
values:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Quoted Attributes ¶
If your attribute name contains odd characters that might interfere with JavaScript syntax, either
quote it using "" or '', or use commas to separate different attributes. Examples of such
characters include [] and () (frequently used in Angular 2).
//- In this case, `(click)` is treated as a
//- function call instead of a attribute name,
/- resulting in the unusual error.
div(class='div-class' (click)='play()')
index.pug:4:11
2| //- function call instead of a attribute name,
3| //- resulting in the unusual error.
> 4| div(class='div-class' (click)='play()')
-----------------^
Syntax Error: Assigning to rvalue
div(class='div-class', (click)='play()')
div(class='div-class' '(click)'='play()')
Attribute Interpolation ¶
Caution
Previous versions of Pug/Jade supported an interpolation syntax such as:
a(href="/#{url}")Link
This syntax is no longer supported. Alternatives are found below. (Check our migration guide for more
information on other
incompatibilities between Pug v2 and previous versions.)
The case statement is a shorthand for JavaScript’s switch statement. It takes the following
form:
Case Fall Through ¶
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
If your JavaScript runtime supports ES2015 template strings (including Node.js/io.js 1.0.0 and
later), you can use that syntax for attributes. This is really useful for attributes with really
long
values:
Block Expansion ¶
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
If your JavaScript runtime supports ES2015 template strings (including Node.js/io.js 1.0.0 and
later), you can use that syntax for attributes. This is really useful for attributes with really
long
values:
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
Unbuffered code starts with -. It does not directly add anything to the output.
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
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:
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unbuffered Code
¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Unescaped Buffered Code ¶
Pug provides four ways of getting plain text — that is, any code or text content that should
go,
mostly unprocessed, directly into the rendered HTML. They are useful in different
situations.
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
Pug allows you to write inline JavaScript code in your templates. There are three types of
code:
Unbuffered, Buffered, and Unescaped Buffered.
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Block Expansion
¶
Self-Closing Tags ¶
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:
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "Timothy's Pug source code!
"
Rendered Whitespace
¶
Pug is a community run open source project, and we’d love to help you get started.
Stack Overflow ¶
For crowdsourced technical questions from expert Pug devs in our community. Also frequented by
the Pug core team.
Post a question
Twitter ¶
Receive the latest news on Pug
Follow us
GitHub ¶
We use GitHub issues exclusively as a bugs and feature requests tracker. If you think you have
found a bug, or have a new feature idea, please start by making sure it hasn’t already been
reported or fixed. You can search through existing issues and pull requests to see if
someone
has reported one similar to yours.
Open an issue
Tidelift subscription ¶
Pug and the maintainers of thousands of other packages are working with Tidelift to deliver
one enterprise subscription that covers all of the open-source you use.
If you want the flexibility of open-source and the confidence of commercial-grade software, this
is worth looking at.
The Tidelift Subscription manages your dependencies for you:
- Get the tools you need to continuously catalog and understand the open-source software that
your application depends on.
- Your subscription helps pay the open-source community maintainers of the packages you use,
to ensure they meet the standards you require.
- Address issues proactively, with tools that scan for new security, licensing, and
maintenance issues, and alert participating open-source maintainers so they can resolve them
on your behalf.
- Tidelift helps measure and improve your open-source dependencies’ health – which improves
your app’s health – and gives a shortlist of high-impact steps your team can take to improve
them even more.
- Get commercial assurances that don’t come for free with open-source packages, such as
intellectual property indemnification and support under a service level agreement. You
expect these guarantees from proprietary software, and you can get them when using
open-source as well.
The end result? All of the capabilities you expect from commercial-grade software, for the
full
breadth of open-source you use. That means less time grappling with esoteric
open-source
trivia, and more time building your own applications – and your business.
Get
more details
Request
a demo
Custom Work
¶
Tweak Pug to meet specific requirements. Give us a summary of your needs and we’ll help you
if
we can.
Note that work must be Pug related. We don’t accept general development work. Our
contracting
price is $200/hour or $1,500/day.
Send us an email
You have reached the end of the documentation!
Of course, this is a layout exercise, and some chapters have been paste/colled for the
sake
of conciseness.
All the documentation on this page is taken from PugJs
Certification Project, Technical Documentation Page, freeCodeCamp(🔥)
Powered by z-bj