Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Difference between React.js and Node.js
Next article icon

Difference between index.ejs and index.html

Last Updated : 09 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

What is HTML?

HTML (Hypertext Markup Language) is used to design the structure of a web page and its content. HTML is actually not technically programming languages like C++, JAVA, or python. It is a markup language, and it ultimately gives declarative instructions to the browser. When a web page is loaded, the browser first reads the HTML and constructs DOM (Document Object Model) tree from it. HTML consists of a series of elements.

What is a Template Engine? 

A template engine is a tool that enables developers to write HTML markup using plain JavaScript. The template engine has its own defined syntax or tags that will either insert variables or run some programming logic at run time and generate final HTML before sending it to the browser for display. 

 

What is EJS?

EJS simply stands for Embedded JavaScript. It is a simple template language or engine. EJS has its own defined syntax and tags. It offers an easier way to generate HTML dynamically.

Relation between HTML and EJS:

We actually define HTML in the EJS syntax and specify how and where various data will go on the page. Then the template engine combines these data and runs programming logic to generate the final HTML page.  So, the task of EJS is to take your data and inserts it into the web page according to how you've defined the template and generate the final HTML. 

For example, you could have a table of dynamic data from a database, and you want EJS to generate the table of data according to your display rules.

When to use HTML/ EJS?

It varies according to your application. If you want to render a static page then go for an HTML file and if you want to render a dynamic page where your data coming from various sources then you must choose an EJS file.

Difference between index.ejs and index.html:
HTML fileEJS file
Good for the static web page.Good for the dynamic web page.
HTML makup language is used to write HTML file.JavaScript is used to write EJS files.
Written in HTML language.HTML markup generates dynamically using JavaScript.
It could not bind dynamic data before sending it to the browser.Dynamic data will bind with the template then the final output will send to the browser.
Extension of HTML file is .htmlExtension of EJS file is .ejs

Example: Assume you have a web page to show employee's salaries. If you create a static HTML file then you need to rewrite it every time when new employee added or salary changes. But, with EJS it becomes too easy to insert dynamic data into it. 

HTML Version Code:

index.html
<!DOCTYPE html> <html>  <head>     <title>Employee Salary</title>     <style>         table {             margin: 20% auto;             border-collapse: collapse;             border-spacing: 0;             width: 20%;             border: 1px solid #ddd;         }                  th,         td {             text-align: left;             padding: 16px;         }                  tr:nth-child(even) {             background-color: #d1d1d1;         }     </style> </head>  <body>     <table>         <tr>             <th> Employee </th>             <th> Salary </th>         </tr>         <tr>             <td> Sayan Ghosh </td>             <td> 37000 </td>         </tr>         <tr>             <td> Susmita Sahoo </td>             <td> 365000 </td>         </tr>         <tr>             <td> Nabonita Santra </td>             <td> 36000 </td>         </tr>         <tr>             <td> Anchit Ghosh </td>             <td> 30000 </td>         </tr>     </table> </body>  </html> 

Output:

EJS Version Code:

Step 1: First, create a NodeJS application and install the required modules like express.js and ejs modules.

Note: Refer to https://www.geeksforgeeks.org/how-to-use-ejs-in-javascript/ to get started with EJS.

Step 2: Create an index.js file which is our basic server with the following code.

index.js
var express = require('express'); var app = express();  // Set EJS as templating engine  app.set('view engine', 'ejs');  // Employees data const empSalary = [     {         name: "Sayan Ghosh",         salary: 37000     },     {         name: "Susmita Sahoo",         salary: 365000     },     {         name: "Nabonita Santra",         salary: 36000     },     {         name: "Anchit Ghosh",         salary: 30000     } ]  app.get('/employee/salary', (req, res) => {      // Render method takes two parameter     // first parameter is the ejs file to      // render second parameter is an      // object to send to the ejs file     res.render('index.ejs', { empSalary: empSalary }); })  // Server setup app.listen(3000, function (req, res) {     console.log("Connected on port:3000"); }); 

Step 3: Create an index.ejs file and put it in the views folder with the following code.

index.ejs
<!DOCTYPE html> <html> <head>   <title>Employee Salary</title>   <style>     table {     margin: 20% auto;     border-collapse: collapse;     border-spacing: 0;     width: 20%;     border: 1px solid #ddd;   }    th, td {     text-align: left;     padding: 16px;   }      tr:nth-child(even) {    background-color: #d1d1d1;   }   </style> </head> <body>     <table>       <tr>            <th> Employee </th>           <th> Salary </th>       </tr>        <% empSalary.forEach(emp => { %>               <tr>                   <td> <%= emp.name %>                   <td> <%= emp.salary %>               </tr>       <% }); %>        </table> </body> </html> 

Step 4: Run the server using the following command:

node index.js

Output: Now open your browser and go to http://localhost:3000/employee/salary, you will see the following output:


Next Article
Difference between React.js and Node.js

B

braktim99
Improve
Article Tags :
  • Difference Between
  • Web Technologies
  • HTML
  • Node.js
  • HTML-Tags
  • HTML-Questions
  • EJS-Templating Language
  • Web Technologies - Difference Between

Similar Reads

  • Difference Between Node.js and Asp.net
    ASP.NET: It is an open-source web application framework initially discharged by Microsoft in January 2002 with the primary cycle of the .NET system. It is built on the Common Dialect Runtime (CLR), permitting the utilization of any .NET dialect counting C# (object-oriented), F# (utilitarian to begin
    4 min read
  • Difference between React.js and Node.js
    1. React.js : This is the Javascript library that was developed by Facebook in 2013. This library was developed in order to improve and enhance the UI for the web apps but with time it has evolved a lot. It is open-source and supports different inbuilt functions and modules like form module, routing
    2 min read
  • Difference between Node.js and React.js
    Node.js and React.js are two powerful technologies widely used in the development of modern web applications. While both are based on JavaScript, they serve entirely different purposes and are used at different stages of the web development process. This article provides a detailed comparison betwee
    5 min read
  • Difference between JSP and HTML
    1. Java Server Pages (JSP) : JSP stands for Java Server Pages. These files have the extension. jsp. The main advantage of JSP is that the programmer can insert Java code inside HTML. There are JSP tags to insert Java code. The programmer can write the tag at the end of the Java code. There are diffe
    3 min read
  • Difference between HTML and HTTP
    HTML stands for HyperText Markup Language and is one of the basic tools any webmaster or web designer uses while HTTP stands for HyperText Transfer Protocol and is a tool used in browsing the web. It would be helpful for anyone designing web sources to clearly understand the relation between HTML an
    5 min read
  • Difference Between WWW and Public_HTML
    Computers all over the world are connected by a huge global network called the Internet. People can share information and interact via the Internet from any location around the world. There are innumerable websites on the internet that may be accessed using various web browsers. These websites and w
    4 min read
  • Difference between Node.js and JavaScript
    JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
    3 min read
  • Difference between ES5 and ES6
    ECMAScript (ES) is a standardized scripting language specification developed by Ecma International. It serves as the foundation for JavaScript and many other scripting languages. Over time, different versions of ECMAScript have been released, with ES5 (ECMAScript 5) and ES6 (ECMAScript 6) being two
    3 min read
  • Difference between HTML and HTML5
    HTML stands for Hyper Text Markup Language. It is used to design web pages using a markup language. HTML is a combination of Hypertext and Markup language. Hypertext defines the link between the web pages. A markup language is used to define the text document within the tag which defines the structu
    4 min read
  • Difference between ERP and ERP II
    1. Enterprise Resource Planning (ERP): Enterprise Resource Planning is the foundation system for domestic and global operations, supporting most or all functional areas in their daily operations. is one of the more common categories of business software, especially with large-scale businesses. It is
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences