【问题标题】:Compound if statements in ejs templateejs模板中的复合if语句
【发布时间】:2020-01-02 04:28:44
【问题描述】:

是否可以在 ejs 模板中使用复合 if 语句:

<% if(this.thing == 'winner' || that.thing == 'winner') { %>

我知道你可以在 ejs 中使用 if 语句。我正在专门寻找不能在所有 js 模板方案中使用的 compound if 语句的示例。

【问题讨论】:

标签: javascript ejs


【解决方案1】:

当然可以——

<% x = 5 %>

<% if(x === 4 || x === 5) { console.log('foo')}%>

【讨论】:

    【解决方案2】:

    是的,ejs 支持复合语句:

    <%
        var fruits = ["Apple", "Pear", "Orange", "Lemon"];
    %>
    
    These fruits are amazing:
    <% for(var i = 0; i < fruits.length; ++i) {%>
        <% if (fruits[i] == 'Apple' || fruits[i] == 'Lemon') {%>
            - <%=fruits[i]%>s
       <% }%>
    <% } %>
    

    输出:

    These fruits are amazing:
    
            - Apples
    
            - Lemons
    

    谢谢@JakeHolzinger

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2016-11-26
      • 2012-09-04
      • 1970-01-01
      • 2014-11-21
      相关资源
      最近更新 更多