20

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"

halfer
  • 19,824
  • 17
  • 99
  • 186
Sandromedeiros
  • 367
  • 1
  • 4
  • 9

4 Answers4

36

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

Community
  • 1
  • 1
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
4

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>
<% } %>
2

Yes , Here is short hand version :

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

0

Do you mean something like this...?

<% if (kindOfDay === "Sarturday" || kindOfDay === "Sunday") { %>
  <h1 style="color: green;"><%= kindOfDay %> Todo List</h1>
<% } else { %>
  <h1 style="color: blue;"><%= kindOfDay %> Todo List</h1>
<% } %>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31