简体   繁体   English

如何在 ejs 中有条件地包含视图?

[英]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 .当我的会话变量person不是null或不是undefined时,我只想在我的ejs 模板中显示仪表板

But it's just showing the dashboard every time.但它每次都只显示仪表板。 and in my log statement, the person value is undefined .在我的日志语句中, person值是undefined

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

What could be the proper way to do it?什么是正确的方法呢?

I'm not sure but can you try changing the if statement to我不确定,但你可以尝试将 if 语句更改为

<% if (person) { %>

It will simple return true if person is not null or undefined如果 person 不为空或未定义,它将简单地返回 true

它可以通过以下方式完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM