Embedded Javascript
This sandbox is in the article namespace. Either move this page into your userspace, or remove the {{User sandbox}} template.
Lead
EJS stands for Embedded JavaScript. It is a web templating system or templating language that allows developers to code HTML markup with simple JavaScript. Unlike other engines that use templates, EJS is very simple, light, fast, flexible and it is an efficient tool for rendering templates on the server side. It mainly uses logic from JavaScript, which makes benefits for developers who already know JavaScript language before. EJS is frequently used in Node.js, it means that any platform that supports JavaScript language also supports EJS.
Sidebar
- Original publisher: Matthew Earnisse
- Initial release: Feb 2011
- Repository: https://github.com/mde/ejs
- Type: Web Template System, Templating language
- Website: https://ejs.co
History
EJS was first published in February 2011 by Matthew Eernisse, also known as mde on GitHub. Eernisse designed EJS to be a simple, light, fast and flexible templating engine for Node.js, and it allows developers to embed JavaScript logic directly into HTML. EJS is open-source for everyone and continues updates and improvements by Eernisse and other contributors in the Node.js community through the GitHub repository.
Early Development
EJS was inspired by templating systems like ERB ( also known as Embedded Ruby) used in Ruby on Rails, which also allows code embedding within HTML. ELS was created for JavaScript developers to create server-rendered HTML pages in an easy and familiar way, likely other templating engines available in other programming ecosystems.
Evolution
As EJS became more popular, it gradually added new features such as subtemplates that can be included in other templates and caching to improve performance to make it run faster. However, EJS is still simple, light, and fast despite these improvements. These will decrease the feeling of being overwhelmed by complexity of the developers.
Present Day
Eernisse and other contributors continue updates and improvements on GitHub consistently, making sure that it can be compatible with the latest versions of Node.js and new JavaScript features. Because of this, many developers choose EJS as a simple and effective option for rendering HTML on the server side in JavaScript
Example
Initialize Node.js with cli
npm init -y
Then install Express.js and EJS with cli
npm i express ejs
Go to package.json and add "type": "module",
Create index.js then config server file with import, set view engine and listening port
then use the GET method to the ‘/’ endpoint then return with the GET with this code.
It will look like the code below.
This code will render and send user data to an index.ejs, then create folder name views, then create an EJS file, and put this code in it.
It will receive data user that we send from the server. We can use it with
<%= user.name %>
After finishing these steps, you have to run Node in cli using node index.js Then go to http://localhost:3000/ or replace 3000 with your port