A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client.
This approach makes it easier to design an HTML page. Express uses Jade as its default template engine. Express supports other template engines also, like PUG, Mustache, EJS, Dust.js, Nunjucks.
Jade has been renamed to PUG. You can continue to use Jade in your app, and it will work just fine. Each template engine has its own Pros and Cons, such as Jade supports macros and white space significant indentation but not suitable for Non-HTML output.
Mustache.js operates on plain HTML, has no significant white space and could be used for things other than HTML, like config files, JSON. But, it has some limitations like no built-in layout support and no Streaming supports.
Dust.js supports Async & streaming, Filter supports but it can’t automatically load partials and Preloading and naming templates for rendering feels strange. ejs template engine can be used for things other than HTML, like config files, JSON, and it provides Filters support but no async support. Also, it has very verbose syntax.
So, it totally depends upon a company and its requirements what they want to use as their template engine. Even multiple template engines can be used. In this and next upcoming articles, I will prefer to use Jade(PUG) as our template engine and toady, I will explain Jade(PUG) Template Engine.
Install PUG Template Engine
Use “npm install –save pug” to install all dependencies for PUG template engine.
After successful installation of PUG packages, now we create set up for “pug” template engine. First, create a “views” folder in your project. In this folder, we will insert all our template layouts.
Now, add the following lines in your project.
app.set('view engine', 'pug');
app.set('views', './views');
In the first line, we define the view engine for project, which is “pug” template engine for our project. In second line, we define the paths for template layouts, where Express will search for the template layouts.
Now, create a “demo.pug” file in Views folder and add the following code in this file.
- doctype html
- html
- head
- title = "Hello Pug"
- body
- h1 This is demo example
- p We implmented Pug template engine
Now, run the application and you will get the following output.
What is the role of PUG view engine in this example? Actually, PUG converts the markup language to HTML code. It takes the markup code from “demo.pug” file and renders it to the HTML code. It generates the following HTML code for the “demo.pug” markup code.
- <!DOCTYPE html>
- <html>
-
- <head>
- <title>Hello Pug</title>
- </head>
-
- <body>
- <h1>This is demo example</h1>
- <p>We implmented Pug template engine</p>
- </body>
-
- </html>
Sending values to templates
Now, let's learn how to send and use the parameters in templates.
Demo.pug
- doctype html
- html
- head
- title Hello Pug "
- body
- h1 = name
- p = city
- p = job
Index.js
- var express = require('express');
- var app = express();
- app.set('view engine', 'pug');
- app.set('views', './views');
- app.get('/demo', function(req, res) {
- res.render('demo', {
- name: " My Nam is Pankaj Choudhary",
- city: "I live in Alwar",
- job: "I am working as a Software Developer"
- });
- });
- app.listen(1400);
Output
In this example, we send some parameters to template file and use the parameters value using "=" (equal) sign. You can also insert the value of parameters into a text line using #(variable name). Let’s take an example.
Demo.pug
- doctype html
- html
- head
- title Hello Pug
- body
- h1 My name is# {
- name
- }
- p I live in #{
- city
- }
- pI am working as a# {
- job
- }
Index.js
- var express = require('express');
- var app = express();
- app.set('view engine', 'pug');
- app.set('views', './views');
- app.get('/demo', function(req, res) {
- res.render('demo', {
- name: " Pankaj Choudhary",
- city: "Alwar",
- job: "Software Developer"
- });
- });
- app.listen(1400);
Output
Indentation is more important in PUG View Engine. It means, all the elements in a template must contain proper indentation otherwise error or unexpected result will be found. The tags that have same indentation must be on same level. For example, if body and head tag have same indentation, so both tags should have the same level. You can use either space or tabs to indent the tags but both at a time. I will prefer to use the “tab” to indent the tags in template. We can put the text inside tag using three methods.
Using Space
h1 My name is Pankaj
Using Pipes
div
My Name is Pankaj | I live in Alwar | I am working as Software Developer
Block of text
div.
My Name is Pankaj.
Working as a Software developer
Attributes in Template
We can assign the list of comma separated attributes for an element, using the parenthesis.
Example
div(style="color:red;font-size:24px",id="div1").
My Name is Pankaj.
Working as a Software developer
The above code will generate the below code in HTML.
- <div style="color:red;font-size:24px" id="div1">My Name is Pankaj. Working as a Software developer</div>
Output
Include the Components
PUG provides a better way to manage the reusable code. In PUG, we create a separate file for reusable code and implement that file using the “require” attribute wherever needed. Now, create another “data.pug” file and insert the below data into that file.
My Name is Pankaj.
Working as a Software developer.
Demo.pug
- doctype html
- html
- head
- title Hello Pug
- body
- div(style = "color:red;font-size:24px", id = "div1")
- include. / data.pug
Output
Iterations
PUG supports two types of iterations - Each and While. Let's take one example for each.
Each Loop
ul
each val in ['Alwar','Delhi','Jaipur','Gurgaon']
li=val
Output
In this example, Each loop generates the list of li elements like below.
- <ul>
- <li>Alwar</li>
- <li>Delhi</li>
- <li>Jaipur</li>
- <li>Gurgaon</li>
- </ul>
While Loop
-
- -
- var n = 0;
- ul
- while n < 10
- li = n++
Output
The above code generates a list of li elements like previous example but here, we use the While loop instead of Each loop.
Conclusion
In this article, we learned about PUG template engine. Express supports various types of template engines like PUG, Mustache, EJS, Dust.js, Nunjucks. You can use any template engine in your project, as per your requirement.
Thanks for reading the article.