If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Implementing partials in web development involves using a templating system to create reusable components that can be included in multiple pages. We'll demonstrate how to implement partials using EJS, a popular templating engine for JavaScript.
Assuming you have Node.js and npm (Node Package Manager) installed, follow these steps:
Step 1: Set Up a New Project Create a new directory for your project and navigate into it.
bashCopy code
mkdir partials-demo
cd partials-demo
Step 2: Initialize a New Node.js Project Run the following command to initialize a new Node.js project and create a package.json
file.
bashCopy code
npm init -y
Step 3: Install EJS Install the EJS templating engine using npm.
bashCopy code
npm install ejs
Step 4: Create the Partial Files In your project directory, create a folder named views
to store your EJS templates. Inside the views
folder, create two EJS files: header.ejs
and footer.ejs
.
bashCopy code
mkdir views
touch views/header.ejs
touch views/footer.ejs
Edit the header.ejs
file:
htmlCopy code
<!-- views/header.ejs -->
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<header>
<h1>Welcome to our website</h1>
<!-- Other header content goes here -->
</header>
Edit the footer.ejs
file:
htmlCopy code
<!-- views/footer.ejs -->
<footer>
<p>© <%= year %> Your Website. All rights reserved.</p>
<!-- Other footer content goes here -->
</footer>
</body>
</html>
Step 5: Create a Page that Uses Partials Create a new EJS file named index.ejs
in the views
folder. This file will be our main page that includes the header and footer partials.
bashCopy code
touch views/index.ejs
Edit the index.ejs
file:
htmlCopy code
<!-- views/index.ejs -->
<%- include('header', { title: 'Home' }) %>
<main>
<h2>Welcome to our Home Page!</h2>
<p>This is the main content of the home page.</p>
</main>
<%- include('footer', { year: new Date().getFullYear() }) %>
Step 6: Set Up an Express Server Create an app.js
file in your project directory to set up an Express server.
bashCopy code
touch app.js
Edit the app.js
file:
javascriptCopy code
// app.js
const express = require('express');
const app = express();
const path = require('path');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.get('/', (req, res) => {
res.render('index');
});
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Step 7: Start the Server Run the following command to start the Express server:
bashCopy code
node app.js
Step 8: Test the Application Open your web browser and visit http://localhost:3000
. You should see the main page with the header and footer content from the partials.
Congratulations! You have successfully implemented partials using EJS in your web application. By using partials, you can easily reuse components and maintain a consistent layout across multiple pages, making your code more organized and DRY.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.