Locals In Views

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 255:- Locals In Views

In the context of a Node.js web application, "locals" refer to variables that are passed to the view or template engine for rendering. These variables contain the dynamic content that will be displayed to the user, such as data retrieved from a database, user input from a form, or calculations performed by the application.

When rendering a view, the Express framework provides a res.locals object that can be used to pass variables to the view. This object is available to all middleware functions that are called after the res.render() method is called.

Here's an example of using res.locals to pass a variable to a view:

javascriptCopy code

app.get('/', function(req, res) {  res.locals.title = 'My App';  res.render('index'); });

In this example, we set the title variable on res.locals to 'My App'. This variable can then be accessed in the view by using the title variable.

phpCopy code

<!DOCTYPE html> <html>  <head>    <title><%= title %></title>  </head>  <body>    <h1>Welcome to <%= title %></h1>  </body> </html>

In the view, we use the EJS templating engine to interpolate the title variable using the <%= %> syntax. This will render as "Welcome to My App" in the browser.

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