Steps to include a template with parameters in EJS

Step 1: Firstly, we will make the folder named root by using the below command in the VScode Terminal. After creation use the cd command to navigate to the newly created folder.

mkdir root
cd root

Step 2: Once the folder is been created, we will initialize NPM using the below command, this will give us the package.json file.

npm init -y

Step 3: Once the project is been initialized, we need to install Express and EJS dependencies in our project by using the below installation command of NPM.

npm i express ejs

Step 4: Now create the below Project Structure in your project which includes app.js, views/index.ejs, and views/footer.ejs files.

How to include a template with parameters in EJS?

In EJS, we can include templates with parameters for the reuse of modular components in web applications. By passing parameters during the inclusion, we can create interactive templates. In this article, we will see a practical example of including a template with parameters in EJS. Below is the syntax to include a template with parameters in EJS.

Syntax to include template with parameters:

<%- include('template.ejs', {param1: value1, param2: value2, ...}) %> 

Similar Reads

Steps to include a template with parameters in EJS:

Step 1: Firstly, we will make the folder named root by using the below command in the VScode Terminal. After creation use the cd command to navigate to the newly created folder....

Folder Structure:

...

Approach to include template with parameters in EJS:

In the above example, we are passing the information to the EJS template using res.render(‘index’, { data: temp}). We have included a footer template with the dynamic parameters in the main template using <%- include(‘footer.ejs’, { year: new Date().getFullYear() }) %>....