Home > Software design >  EJS conditional rendering
EJS conditional rendering

Time:01-19

How to render html if user is logged in or not. I have tried if, else but it doesn't work as i want it to be. Is there another way of doing the same.

    <% if(typeof(user) === "defined" ){ %>
      <li >
        <a href="/user/<%= user.id %> "> <i > <%= user.name %> </i> </a>
      </li>
  <% } %>
  <% if(typeof(user) != "undefined" ){ %>
          <li >
            <span >
              <a href="/auth/register"> Sign Up</a>
            </span>
          </li>
          <li >
            <span >
              <a href="/auth/login">
                <span> Log In</span>
              </a> </span>
 <% } %> 

CodePudding user response:

 <% if(typeof user !== 'undefined'){ %>
      <li >
        <a href="/user/<%= user.id %> "> <i > <%= user.name %> </i> </a>
      </li>
  <% } else { %>
          <li >
            <span >
              <a href="/auth/register"> Sign Up</a>
            </span>
          </li>
          <li >
            <span >
              <a href="/auth/login">
                <span> Log In</span>
              </a> </span>
 <% } %>
  •  Tags:  
  • Related