How to use Pug with Hapi

This tutorial is compatible with hapi v17. Node and Hapi should be installed for work with this tutorial.

In this quick post I will show how to use a Pug as a default template engine in your Hapi project. We will use information from previous article about how to use handlebars in Hapi. First of all, let’s install the Pug:

Setup template engine and render

You need include it to your server and initialize the default template engine for our server. Use the following code:

Now, we can start creating a new views. Create an index.html file in templates folder with the following content:

As you see, we added dynamic content – title and message. We will interpolate these variables inside GET /index route using the view method:

Full example

You can check more information about how to use Pug in the official guide.

If you liked this, please help spread the word by hitting the recommend button below. Thanks!

Tags:, ,