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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read