![]() |
VOOZH | about |
EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML.
Install the module using the following command:
npm install ejs --save Note:
In the commands above, "npm" stands for the Node Package Manager, which is where all the dependencies are stored. The
"--save" flag is no longer needed after Node 5.0.0, as all the modules we install will now be added to dependencies automatically.
<%= data %><% if(data == "1"){%>
<h5>Cricket</h5>
<%}else{%>
<h5>Football</h5>
<%}%>
let data = '<%-data%>'Now you can perform any operation on the data variable that has the same value as the EJS passed data variable.
Example: Implementation to use EJS in our project.
The default behaviour of EJS is that it looks into the ‘views’ folder for the templates to render. So, let’s make a ‘views’ folder in our main node project folder and make a file named “home.ejs” which is to be served on some desired request in our node project.
The "name" variable has been passed from the server to the 'name.ejs' file and displayed using an h2 tag. To use the "name" variable in the script tag, all we did was declare a variable and assign the EJS variable to the declared variable using:
let name = '<%-name%>'Step to Run Application: Run the application using the following command from the root directory of the project
node index.jsOutput:
👁 Image