i

Templating Engine

A Template engine is basically used to create the template files in an application. The variables in the template file will be replaced with the actual values at runtime, and then the template is transformed into an HTML file. The HTML pages can be easily designed using this approach.

The Express application generator allows configuring different templating engine such as

  • Pug

  • Ejs

  • Twig etc.

Pug is the default template engine for the Express framework. It was earlier called a Jade template engine.

We can use the Pug template engine for generating HTML in a simple way. It uses a simple representation of HTML using simple text instructions.
The main feature of Pug template engine is

  • CSS selector element definitions

  • Extensibility

  • Expressions etc.

Pug syntax is based on whitespaces and indentation and is very easy to learn.

To work with the Pug engine, we need to install modules respective to it.

>npm install pug

To use Pug templating engine in an Express application, we add

Lets see it with a Demo application

Pug-demo.js

In the code snippet above, we kept our pug view in the View folder.\

Index.pug

On executing the Get,

We can see a view just like an HTML page.