How can I use if statement in ejs?

前端 未结 3 1641
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 14:17

I have a page that makes a foreach and show some photos like this

<% imgs.forEach(function(img) { %>
      /s         


        
相关标签:
3条回答
  • 2021-02-07 14:25

    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>
    <% } %>
    
    0 讨论(0)
  • 2021-02-07 14:32

    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

    0 讨论(0)
  • 2021-02-07 14:33

    Yes , Here is short hand version :

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

    0 讨论(0)
提交回复
热议问题