Using template engine in Nodejs
Posted By : Rakesh Chandra | 24-Apr-2020
Template Engine in Express js
The template engine helps us to create HTML templates with minimum HTML code also it can append data into HTML templates at the client-side.
each template language to define HTML template and inject data into it
A Template engine allows us to use static template files in our application. At runtime, the template engine replaces all the variables in the template files with the actual values and transfer the complete template into HTML files to display it to the client.
this approach makes it very easy to design an HTML page.
There are numbers of template engine available in node js
the most popular template engine in node js are pug ,mustache ,and ejs the express app generator use Jade as default ,but it also support another template engine-
Advantage of template engine in node js -
- Improve developer's productivity.
- Improve readability and maintainability.
- Improve the performance of the application,
- A single template for multiples pages.
- the template also can be accessed using CDN.
- You can host the complete application in a single instance.
How to install ejs or pug
use following npm command - to install popular template engines in node js project .
npm ejs or npm install pug
and require ath the template engine pacakes and start using them
- const pug = require("pug")
- const ejs = require("ejs")
Conclusion : -
The template engine helps us to create HTML templates with minimum html code also it can append data into HTML templates at client side .
each template language to define html tempalte and inject data it improve developer's productivity and improve readability and maintainability make code redable and understandable, reduce the HTML code complexity.