Search code examples
javascripthtmlnode.jsexpressejs

How can I use if statement in ejs?


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"


Solution

  • 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