问题
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