Attaullah Khan
Attaullah Khan

Reputation: 370

How to include a view conditionally in ejs?

I only want to show dashboard in my ejs template when my session variable person is not null or not undefined.

But it's just showing the dashboard every time. and in my log statement, the person value is undefined.

<%= console.log(person); %>
<% if (person !== null || person !== undefined) { %>
<%- include("dashboard"); %>
<% } else { %>
<%- include("login"); %>
<% } %>

What could be the proper way to do it?

Upvotes: 0

Views: 312

Answers (2)

Attaullah Khan
Attaullah Khan

Reputation: 370

It can be done in following way:

<%- include(person !== null && person !== undefined ? "dashboard" : "login"); %>

Upvotes: 0

Ozgur Sar
Ozgur Sar

Reputation: 2204

I'm not sure but can you try changing the if statement to

<% if (person) { %>

It will simple return true if person is not null or undefined

Upvotes: 1

Related Questions