How do you do a for loop/for each in EJS?

Tsardines picture Tsardines · Mar 13, 2018 · Viewed 15k times · Source

I know it has to go inside <% %>, but I'm not sure if it's very different from a typical forEach/for loop. The documentation on the EJS site is pretty limited, so I came here.

<% 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>

Answer

Tsardines picture Tsardines · Mar 13, 2018

So here's the example on embeddedjs:

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

And here's what I did:

<% 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>