Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJS conditional rendering

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 class="navlist-item user">
        <a href="/user/<%= user.id %> "> <i class="far fa-user"> <%= user.name %> </i> </a>
      </li>
  <% } %>
  <% if(typeof(user) != "undefined" ){ %>
          <li class="navlist-item">
            <span class="get-started">
              <a href="/auth/register"> Sign Up</a>
            </span>
          </li>
          <li class="navlist-item">
            <span class="get-started">
              <a href="/auth/login">
                <span> Log In</span>
              </a> </span>
 <% } %> 
like image 798
Bhaskar Avatar asked Aug 26 '26 01:08

Bhaskar


1 Answers

 <% if(typeof user !== 'undefined'){ %>
      <li class="navlist-item user">
        <a href="/user/<%= user.id %> "> <i class="far fa-user"> <%= user.name %> </i> </a>
      </li>
  <% } else { %>
          <li class="navlist-item">
            <span class="get-started">
              <a href="/auth/register"> Sign Up</a>
            </span>
          </li>
          <li class="navlist-item">
            <span class="get-started">
              <a href="/auth/login">
                <span> Log In</span>
              </a> </span>
 <% } %>
like image 109
traynor Avatar answered Aug 28 '26 14:08

traynor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!