How to separate Handlebars HTML into multiple files / sections using Node.js ?
Last Updated :
01 Aug, 2024
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
<h1>Navbar</h1>
footer.ejs
<h1>Footer</h1>
Step 7: Inside 'home.ejs' file write some HTML code, and import both handlebars.
<!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>
<%- include('handlebars/navbar') %>
<h1>Home</h1>
<%- include('handlebars/footer') %>
</body>
</html>
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
<!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>
<%- include('handlebars/navbar') %>
<h1>Home</h1>
<%- include('handlebars/footer') %>
</body>
</html>
navber.ejs
footer.ejs
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:
Similar Reads
How to Setup Handlebars View Engine in Node.js ? Handlebars is a template engine that is widely used and easy to use. The pages contain .hbs extension and there are many other template engines in the market like EJS, Mustache, etc. Installation of hbs module: You can visit the link Install hbs module. You can install this package by using this com
2 min read
How to Display Images using Handlebars in Node.js ? In this article, we will discuss how to display images using handlebars in Node.js. You may refer to this article for setting up handlebars View Engine in Node.jsApproachTo display images using Handlebars in Node.js, pass the image URLs to the template from your server. In the Handlebars template, u
3 min read
How to separate the section from another section in HTML ? HTML Section tag defines the section of documents such as chapters, headers, footers, or any other sections. The section tag divides the content into sections and subsections. The section tag is used when requirements of two headers or footers or any other section of documents are needed. Section ta
2 min read
How to use Handlebars to Render HTML Templates ? Handlebars is a powerful template engine that allows us to create dynamic HTML templates. It helps to render HTML templates easily. One of the advantages of Handlebars is its simplicity, flexibility, and compatibility with various web development frameworks.These are the following methods:Table of C
5 min read
How to Separate Routers and Controllers in Node.js ? In a Node.js application, especially when using the Express framework, separating routers and controllers is a common practice to keep the codebase organized, maintainable, and scalable. This separation of concerns ensures that routing logic is kept separate from the business logic, making the appli
4 min read