你如何在 EJS 中执行 for 循环/for each?

Tsa*_*nes 7 html javascript loops partials express

我知道它必须进入 <% %>,但我不确定它是否与典型的 forEach/for 循环有很大不同。EJS 站点上的文档非常有限,所以我来到这里。

<% include ../../partials/header %>

<body>
  <main>
    <h1>List of all quotes</h1>
    <ul>
      <li> <!-- for loop goes here, inside flounder -->
        <%
        all quote stuff goes here
        author
        content
        wrap it in a link to its quote page
      </li>
    </ul>
  </main>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Tsa*_*nes 16

所以这是关于embeddedjs的例子:

    <ul>
<% for(var i=0; i<supplies.length; i++) {%>
   <li><%= supplies[i] %></li>
<% } %>
</ul>
Run Code Online (Sandbox Code Playgroud)

这就是我所做的:

<% include ../../partials/header %> <
<body>
  <main>
    <h1>List of all quotes</h1>
    <ul>
      <% for(var i = 0; i < author.length; i++) {
        <li><%= author[i] %></li>
      <% } %>

      <% for(var i = 0; i < content.length; i++) {
        <li><%= content[i] %></li>
      <% } %>

    </ul>
  </main>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)


kip*_*uto 8

假设您有一个学生 JSON 对象,其中包含学生姓名、年份和课程的详细信息,然后您可以按如下方式使用 forEach 循环。

<ul>
 <% students.forEach(function(student) { %>
    <li> Name:<%= student.name %> Course:<%= student.course %></li>
 <% }); %> 
</ul>
Run Code Online (Sandbox Code Playgroud)

这同样适用于您的作者和上面的引用问题