node.js - How do i pass a user object to a partials in ejs? -


i'm trying render user's name on navbar, navbar in partial folder , none of routes in node.js renders navbar.ejs because partial. i'm using ejs-mate library having reusable layout component used other's html components don't need copy , paste codes (following dry principle), why? because ejs doesn't have built in reusable layout component's feature, , other libraries not maintained other developers.

currently

i have 2 routes

app.get('/', passport.isauthenticated, function(req, res) {    res.render('home' {      user: req.user    }); });  app.get('/anotherroute', function(req, res) {    res.render('test'); }); 

i have 2 ejs files

home.ejs

<%- layout('boilerplate') %>  <h1>home page</h1> 

another.ejs

<%- layout('boilerplate') %>  <h1>another page</h1> 

those 2 ejs files using boilerplate.ejs layout, don't copy , paste same codes on , on again.

boilerplate.ejs - in file have include navbar , other partials.

<!doctype html> <html lang="en"> <head>      <% include ./partials/navbar %> </head> <body>      <header>         <% include ./partials/header %>     </header>  <div class="container">     <main>         <%- body -%>     </main>      <footer>         <% include ./partials/footer %>     </footer>   </div>  </body> </html> 

this navbar file, , take @ navbar-right class,

if user logged in render name  else render , signup 

navbar.ejs

<nav class="navbar navbar-default" role="navigation">     <div class="container-fluid">          <div class="navbar-header">             <a class="navbar-brand" href="/">                 startupclone             </a>         </div>          <ul class="nav navbar-nav navbar-right">             <li><a href="/">about</a></li>             <% if (!user) { %>              <li><a href="/signup">signup</a></li>             <li><a href="/login">login</a></li>         </ul>         <% } else{ %>           <li><a><%= user.profile.name %></a></li>        <% } %>     </div> </nav> 

the problem right working route pass user's object not other routes. know have pass user's object every single route not because again have copy , paste codes again in every single route against dry principle,

imagine if have 40 routes need use navbar, nightmare me :(

if guys have idea on how hack this, feel free lend thoughts on how tackle problem.

it compulsory pass object trying access on view. 1 thing store user object in session , in view check session user object. way have pass session object atleast use trick problem.

in middleware passport.isauthenticated on succesful login save user object in session

module.exports.isauthenticated = function(req, res, next) {   /*      check valid credentials    */   if (success)   {     req.session.user = founduserinfo;   }   next(); } 

and in partial view

<!-- navbar.ejs --> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid">      <div class="navbar-header">         <a class="navbar-brand" href="/">             startupclone         </a>     </div>      <ul class="nav navbar-nav navbar-right">         <li><a href="/">about</a></li>         <% if (!session.user) { %>          <li><a href="/signup">signup</a></li>         <li><a href="/login">login</a></li>     </ul>     <% } else{ %>       <li><a><%= session.user.profile.name %></a></li>    <% } %> </div> 

now have pass session object each route.

app.get('/', passport.isauthenticated, function(req, res) {    res.render('home' {       user: req.user,       session: req.session    }); });  app.get('/anotherroute', function(req, res) {    res.render('test', {session: null}); }); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -