express ejs layout use different layout

To use different layouts in Express with EJS, you can follow the steps below:

  1. Install Express and EJS: First, make sure you have Express and EJS installed in your project. You can do this by running the following command in your terminal: npm install express ejs

  2. Set up your Express application: Create a new file, for example, app.js, and require the necessary modules: javascript const express = require('express'); const app = express();

  3. Configure the view engine: Set EJS as the view engine for your Express application by adding the following code: javascript app.set('view engine', 'ejs');

  4. Create multiple layout files: Create different layout files for your views. For example, you can have a main-layout.ejs file and a admin-layout.ejs file.

  5. Create the views: Create your views using EJS syntax and specify the layout file to be used for each view. For example, in a file named home.ejs, you can include the following line to use the main-layout.ejs file as the layout: ```ejs <% layout('main-layout') %>

    Welcome to the home page!

```

  1. Render the views: In your Express routes, render the views and pass any necessary data. For example: javascript app.get('/', (req, res) => { res.render('home'); });

  2. Start the server: Finally, start the Express server by adding the following code: javascript app.listen(3000, () => { console.log('Server started on port 3000'); });

By following these steps, you will be able to use different layouts in Express with EJS. Each view can have its own layout, allowing you to customize the design and structure of your application based on specific requirements.