NodeJs : Use the locals object of Express

In previous post we discussed how to pass information to templates using the render() of Express.

In most of websites there is probably some information that you want to pass to all of the templates. With Express we can use the locals object to pass the information that we want any of our templates to access.

Here is how we use the locals object to pass the information

app.locals.siteTitle="My Website";

Information we passed, In this example siteTitle can be accessed like this in any template.

<!DOCTYPE>
<html>
<head>
      
</head>
<body>
   <h1><%=siteTitle %></h1>
  <p>consectetur adipiscing elit. Cras eget dapibus justo. etra justo.</p>  
</body>
</html>

No comments:

Post a Comment