Setting Up A Template Engine

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 250:- Setting Up A Template Engine

To set up a template engine in an Express.js application, we need to follow these steps:

  1. Install the template engine module using NPM. For example, if we want to use the EJS template engine, we can install it with the following command:

    Copy code

    npm install ejs

  2. Require the template engine module in our server file:

    javascriptCopy code

    const ejs = require('ejs');

  3. Set the view engine to the name of the template engine:

    cCopy code

    app.set('view engine', 'ejs');

  4. Create a views directory in the root of our project and add the template files with the extension of the template engine we are using. For example, if we are using EJS, the extension will be .ejs.
  5. Use the render method of the response object to render the template file and pass data as a second argument. For example:

    kotlinCopy code

    app.get('/', (req, res) => {  const data = { name: 'John' };  res.render('index', data); });

    In this example, we are rendering the index.ejs template file and passing the data object to it.

The above steps assume that we are using the EJS template engine. If we are using a different template engine, the steps may vary slightly.

29. My First Express App - A List Of Contacts!

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

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

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support