Implementing Partials

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 416:- Implementing Partials

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>&copy; <%= 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.

49. Beginning The Major Project - 2

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?