Using Pug

Jade has been renamed to Pug. You can continue to use Jade in your app, and it will work just fine. However if you want the latest updates to the template engine, you must replace Jade with Pug in your app.

To render template files, set the following application setting properties, set in app.js in the default app created by the generator:

  • views, the directory where the template files are located. Eg: app.set(‘views’, ‘./views’). This defaults to the views directory in the application root directory.
  • view engine, the template engine to use. For example, to use the Pug template engine: app.set(‘view engine’, ‘pug’).

Then install the corresponding template engine npm package; for example to install Pug:

$ npm install pug –save

Express-compliant template engines such as Jade and Pug export a function named __express(filePath, options, callback), which is called by the res.render() function to render the template code.

Some template engines do not follow this convention. The Consolidate.js library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express.

After the view engine is set, you don’t have to specify the engine or load the template engine module in your app; Express loads the module internally, as shown below (for the above example).

app.set(‘view engine’, ‘pug’)

Create a Pug template file named index.pug in the views directory, with the following content:

html

head

title= title

body

h1= message

Then create a route to render the index.pug file. If the view engine property is not set, you must specify the extension of the view file. Otherwise, you can omit it.

app.get(‘/’, function (req, res) {

res.render(‘index’, { title: ‘Hey’, message: ‘Hello there!’ })

})

When you make a request to the home page, the index.pug file will be rendered as HTML.

Note: The view engine cache does not cache the contents of the template’s output, only the underlying template itself. The view is still re-rendered with every request even when the cache is on.

Pug Working

The general rendering process of Pug is simple. 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, and voilà!, 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.

//- template.pug

p #{name}’s Pug source code!

const pug = require(‘pug’);

// Compile the source code

const compiledFunction = pug.compileFile(‘template.pug’);

// Render a set of data

console.log(compiledFunction({

name: ‘Timothy’

}));

// “<p>Timothy’s Pug source code!</p>”

// Render another set of data

console.log(compiledFunction({

name: ‘Forbes’

}));

// “<p>Forbes’s Pug source code!</p>”

Pug also provides the pug.render() family of functions that combine compiling 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.

const pug = require(‘pug’);

// Compile template.pug, and render a set of data

console.log(pug.renderFile(‘template.pug’, {

name: ‘Timothy’

}));

// “<p>Timothy’s Pug source code!</p>”

Share this post
[social_warfare]
Template Engines
EJS

Get industry recognized certification – Contact us

keyboard_arrow_up