How can I use if statement in ejs?

Sandromedeiros picture Sandromedeiros · Dec 17, 2016 · Viewed 26.9k times · Source

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"

Answer

Shaharyar picture Shaharyar · Dec 17, 2016

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