Skip to main content

Pug

Pug is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It was formerly known as "Jade".

Usage

There are 2 modes for rendering:

Pre-rendered (Default)

The values of the expressions are evaluated and added to the template during compilation of the result page.

The values of all expressions should be supplied in advance using custom settings to the property template.data which accepts an object of key-value pairs.

Example: This provides the value of the expression name

Custom Settings
{
"template": {
"data": {
"name": "LiveCodes"
}
}
}

Full example below

Dynamic

To use this mode, the property template.prerender in custom settings should be set to false.

Example:

Custom Settings
{
"template": {
"prerender": false
}
}

In this mode, in addition to values supplied in custom settings (see above), expressions can have values that are evaluated during the result page runtime.

This can be achieved in JavaScript (or any language that compiles to it) by assigning window.livecodes.templateData to an object with the data.

Please note that template rendering occurs on page load, so the assignment must occur before that.

Example:

Script Editor (JS)
window.livecodes.templateData = { name: 'LiveCodes' };

Full example below

Language Info

Name

pug

Extensions

.pug, .jade

Editor

markup

Compiler

The official Pug compiler.

Version

pug: v3.0.2

Code Formatting

Using Prettier.

Custom Settings

Custom settings added to the property pug are passed as a JSON object to the pug.compile method during compile. Please check the documentation for full reference.

Please note that custom settings should be valid JSON (i.e. functions are not allowed).

Example:

Custom Settings
{
"pug": {
"pretty": true
}
}

Example Usage

Pre-rendered

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"markup": {
"language": "pug",
"content": "p Hello, #{name}!"
},
"customSettings": {
"template": {
"data": {
"name": "LiveCodes"
}
}
}
},
"params": {
"compiled": "open"
}
};
createPlayground('#container', options);

Dynamic

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"markup": {
"language": "pug",
"content": "p Hello, #{name}!"
},
"script": {
"language": "javascript",
"content": "window.livecodes.templateData = { name: \"LiveCodes\" };"
},
"customSettings": {
"template": {
"prerender": false
}
},
"activeEditor": "script"
}
};
createPlayground('#container', options);