EJS – Embedded Javascript Templating

EJS – Embedded Javascript Templating

The template engine can be used to create a template. It can inject data into the template at the client side and produce the final version.

Installation: Install module using the following command:

npm install ejs –save

The npm is a place where you can install all the dependencies.

The first thing we need to do is to set the templating engine to Express, which is a Node.js web application server framework, which is specifically designed for building a single-page, multi-page, and hybrid web applications. It is the standard server framework.

The data from the server is sent to the EJS file and we can use the below line to get the data.

<%= data %>

The data can be written in the following form if we want to use it in normal operations.

<% if(data == "1"){%>

Cricket

<%}else{%>

Football

<%}%>

All you need to do is pass that data in another variable to access it in the script tag flutter web.

let data = ‘<%-data%>‘

You can perform any operation on the data variable that has the same value.

var express is required; var app is required; app.set is required; app.get is required.

The default behavior of EJS is to look into theviews folder. To make a file named home.ejs, we need to make aviews folder in our main project folder and then make a file named home.ejs.

meta name is “viewport” and the width of the device is 1.

The name variable has been passed from the server to the name.ejs file and we used h2 tag to show it in the script tag.

let name = ‘<%-name%>‘

Steps to run the program:

  • Go to the root directory after creating all the files.
  • Run command prompt in this directory.

You can run your program with the following command.


Leave a Reply

Your email address will not be published.