Saturday, 10 June 2017

Itterate over json keys with only EJS

Using only EJS (in EJS templates in .ejs files for NodeJS), how would I iterate over keys of an object (that's not an array)?

I'm looking for something like this:

The object like so:

the_object = {
    1: "belongs to id 1",
    3: "belongs to id 3",
    12: "belongs to id 12"
}

Some EJS templating like so:

<% for (key, value: the_object) { %>
    <li>the key is: <%= key %></li>
<% } %>

To produce something like:

  • the key is: 1
  • the key is: 3
  • the key is: 12

Is this possible?



via Alexander Kleinhans

No comments:

Post a Comment