How can I use if statement in ejs?

只谈情不闲聊 提交于 2019-12-12 08:43:14

问题


I have a page that makes a foreach and show some photos like this

<% imgs.forEach(function(img) { %>
      <img src="uploads/<%=user.username%>/screenshots/<%= img %>">
 <% }); %>

And I want make a if statement because, in case that not photos to show gives a message like this:

"no photos uploaded"


回答1:


Something like this:

<% if(imgs.length > 0){ %>
    <% imgs.forEach(function(img) { %>
        <img src="uploads/<%=user.username%>/screenshots/<%= img %>">
    <% }); %>
<% } else{ %>  
    <p>no photos uploaded</p>
<% } %>

Reference




回答2:


Yes , Here is short hand version :

<%= role == 'A' ? 'Super Admin' : ? 'Admin' %>




回答3:


The shorthand version is correct, but it does have a syntax error

<%= role === 'admin' ? 'Super Admin' : 'Admin' %>

Or

<% if(role === 'admin'){ %>
    <p>Super Admin</p>
<% } else{ %>
    <p>Admin</p>
<% } %>


来源:https://stackoverflow.com/questions/41202233/how-can-i-use-if-statement-in-ejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!