Open In App

How to separate Handlebars HTML into multiple files / sections using Node.js ?

Last Updated : 19 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about separating Handlebars HTML into multiple files/sections using Node.js and using it on any page that you want. It helps in reducing the repetition of code. For example, instead of adding the whole navbar on each page, you can just make a template of that navbar and, use import it to any page you want. We can make templates and separate Handlebars using EJS Module. EJS stands for Embedded Javascript, it is a templating engine used by Node.js. This helps to create an HTML template with minimal code. 

Install EJS: Locate your root project directory into the terminal and type the command

npm install ejs

Set EJS as view engine: Inside your server file (app.js or server.js), write the following code to set the EJS as the default view engine

app.set('view engine', 'ejs');

Rearrange Your Directories: It is required to rename your file from ‘.html’ to ‘.ejs’ for using EJS inside it. Then you have to move every single ‘.ejs’ file in the views directory inside your root directory. EJS is by default looking for ‘.ejs’ files inside the views folder.

Create Handlebars: Create a folder inside the views folder, let’s call it handlebars, inside this folder, let’s create two handlebars, the first is the ‘navbar.ejs’ where we write Navigation Bar code, and the second is ‘footer.ejs’ where we write Footer code. 

Import Handlebars: Inside your updated .ejs files, you can use handlebars anywhere you want by using the code

<%- include('handlebars/navbar') %>

Example: Let’s implement the above steps to create a simple project which uses handlebars.

Step 1: Create a folder, let’s call it handlebars, and open it inside the code editor and open the terminal.

Step 2: Locate this folder into the terminal & type the command.

npm init -y

It initializes our application & makes a package.json file.

Step 3: Install Express and EJS modules inside the project using the following command.

node install express ejs

Step 4: Create an ‘app.js’ file, inside this file require the express module, and create a constant ‘app’ for creating an instance of the express module then set the EJS as the default view engine.

const express = require('express');
const app = express();
app.set('view engine', 'ejs');

Step 6: Create a views folder, inside this folder create ‘home.ejs’ file, and a handlebars folder, and inside this handlebars folder, creates two files, ‘navbar.ejs’ and ‘footer.ejs’, and write some basic HTML code inside these files.

navbar.ejs

<!DOCTYPE html>
<html lang="en">
<head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content=
           "width=device-width, initial-scale=1.0">
       <title>Home</title>
</head>
<body>
   
<h1>Navbar</h1>

footer.ejs

    <h1>Footer</h1>
</body>
</html>

Step 7: Inside ‘home.ejs’ file write some HTML code, and import both handlebars.

<%- include('handlebars/navbar') %>

<h1>Home</h1>

<%- include('handlebars/footer') %>

Step 7: Inside ‘app.js’, create the get method to render the ‘home.ejs’ file and listen method to listen to this project on port 3000.

const express = require('express');

const app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) =>{
   res.render('home.ejs');
});

app.listen(3000); 

Folder Structure:

 

Complete Code:

app.js




const express = require('express');
  
const app = express();
  
app.set('view engine', 'ejs');
  
app.get('/', (req, res) =>{
    res.render('home.ejs');
});
  
app.listen(3000);


home.ejs




<%- include('handlebars/navbar') %>
  
<h1>Home</h1>
  
<%- include('handlebars/footer') %>


navber.ejs




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
              "width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
      
<h1>Navbar</h1>


footer.ejs




<h1>Footer</h1>
</body>
</html>


Step to run the application: Open the terminal and type the following command.

node app.js

and open the browser, and in the search bar type 

http://localhost:3000/

Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads