npm install - g pug-clidiv.remark p Pug rocks!!The code above produces this:
<div class="remark"> <p>Pug rocks!!</p> </div>
<div> element..className#IDnameh1.navbar-header We can write anything we want here …| character: p
| You are logged in as
| user@example.com
p.
HTML TEXT.....
// //- Invisible comment.
//Visible comment.
pug.compile(source, options)string
?options
function
var pug = require('pug'); // Compile a function var fn = pug.compile('string of pug', options); // Render the function var html = fn(locals); // => '<string>of pug</string>'
pug.render(source, options, callback)render is called, which may impact performance.
string
?options
function
string
var pug = require('pug'); var html = pug.render('string of pug', options); // => '<string>of pug</string>'
| Option | Accepts | Action |
|---|---|---|
filename |
string |
The name of the file being compiled. Used in exceptions, and required for relative include \s and extend\s. Defaults to Pug. |
basedir |
string |
The root directory of all absolute inclusion. |
doctype |
string |
If the doctype is 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. Defaults to undefined. |
self |
boolean |
Use a self namespace to hold the locals. It will speed up the compilation, but instead of writing variable you will have to write self.variable to access a property of the locals object. Defaults to false. |
debug |
boolean |
If set to true, the tokens and function body are logged to stdout. |
compileDebug |
boolean |
If set to true, the function source will be included in the compiled template for better error messages (sometimes useful in development). It is enabled by default, unless used with Express in production mode. |
globals |
Array<string> |
Add a list of global names to make accessible in templates. |
cache |
boolean |
If set to true, compiled functions are cached. filename must be set as the cache key. Only applies to render functions. Defaults to false. |
inlineRuntimeFunctions |
boolean |
Inline runtime functions instead of require-ing them from a shared version. For compileClient functions, the default is true (so that one does not have to include the runtime). For all other compilation or rendering types, the default is false. |
name |
string |
The name of the template function. Only applies to compileClient functions. Defaults to 'template'. |
Pug supports template inheritance. Template inheritance works via the block and extends keywords.
blockblock is simply a "block" of Pug that a child template may replace.
//- layout.pug html head title My Site - #{title} block scripts script(src='/jquery.js') body block content block foot #footer p some footer content
replace (default), prepend, or append blocks.
block append or block prepend, the word “ block” is optional://- page.pug extends layout append head script(src='/vendor/three.js') script(src='/game.js')
extends//- page-a.pug extends layout.pug block scripts script(src='/jquery.js') script(src='/pets.js') block content h1= title - var pets = ['cat', 'dog'] each petName in pets include pet.pug
-). It doesn’t directly add anything to the output, but its values may be used from within Pug.=). It evaluates a JavaScript expression and outputs the result.String interpolation is the process of replacing one or more placeholders in a template with a corresponding value.
Another is using #{}. Here, Pug will evaluate any code between the curly brackets, escape it, and render it into the template
| Syntax | Action |
|---|---|
res.render(path, variables) |
Searches for a pug file to render at path "path", and passes "variables" to it |
#{variable} |
Interpolates "variable" inline with the surrounding Jade code, after evaluating "variable" |
!{variable} |
Interpolates "variable" inline with the surrounding Jade code, without evaluating "variable". |
#[element] |
Interpolates "element" inside of an existing Pug HTML element. Syntax of interpolated HTML elements is identical to that of normal HTML elements. |
| Parameter | Details |
|---|---|
| path | Used in res.render. This is the path of the Pug file that we are going to render. The path is taken from the root of the folder set on your Express app: app.set("views", "templates/views"). For example, res.render("index") will search for a Pug file at templates/views/index.pug. Subdirectories can be specified too; res.render("admin/index") looks for a Pug file at templates/views/admin/index.pug. |
| variables | Used in res.render. A JavaScript object of variables to be made accessible to the Pug file defined by path (above). Within the Pug file, the keys of the above JavaScript object become available as variables. If variables = {title: "Hello", color: "red"}, we could use the title and color variable. Subproperties of nested objects are also available. |
| variable | Used in bracket syntax #{} or !{}. The value of variable will be output in the context of its surrounding Pug code. If a pound symbol is prepended to the opening curly bracket, variable will be evaluated before being output. If an exclamation point is prepended to the opening curly brace, variable will not be evaluated. |
| element | Used in square bracket syntax #[]. The HTML element (in Pug syntax, not normal HTML syntax) will be evaluated and output inline with the surrounding Pug code. |
each and while.
eachul each val in [1, 2, 3, 4, 5] li= valCapture index as you iterate:
ul each val, index in ['zero', 'one', 'two'] li= index + ': ' + valIterate over the keys in an object:
ul each val, key in {1: 'one', 2: 'two', 3: 'three'} li= key + ': ' + valAdd an
else block in the case that the array or object are empty- var values = []; ul each val in values li= val else li There are no values
whilewhile keyword:- var n = 0; ul while n < 4 li= n++