Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 534
Sandromedeiros Avatar asked Dec 17 '16 19:12

Sandromedeiros


2 Answers

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

like image 64
Shaharyar Avatar answered Oct 12 '22 08:10

Shaharyar


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>
<% } %>
like image 3
Alias Nidhin Peter Avatar answered Oct 12 '22 08:10

Alias Nidhin Peter