Nested lists in layouts

Newbie question, I understand how to include simple lists in layouts with:

<h1>Friends</h1>
<ol>
<% data.people.friends.each do |f| %>
<li><%= f %></li>
<% end %>
</ol>

but is there a simple way to include nested lists for data such as:

people:
  - family
     - tom
     - dick
     - harry
  - friends
     - tim
     - duck
     - hairy
     - dog
  - enemies
     - georgew
     - billg

thanks.

First, I propose to remove the people from the YML file, as it’s unnecessary. I assume, the file will be named people.yml, so we only put a comment for clarity. Add colons for each group.

# people
- family:
  - tom
  - dick
  - harry
- friends:
  - tim
  - duck
  - hairy
  - dog
- enemies:
  - georgew
  - billg

Then the code:

<h1>People</h1>
<ol>
<% data.people.each do |group| %>
<li><%= group.flatten[0] %></li>
<ul><% group.flatten[1].each do |person| %>
<li><%= person %></li>
<% end %>
</ul>
<% end %>
</ol>

The output:

<h1>People</h1>
<ol>
  <li>family</li>
  <ul>
    <li>tom</li>
    <li>dick</li>
    <li>harry</li>
  </ul>
  <li>friends</li>
  <ul>
    <li>tim</li>
    <li>duck</li>
    <li>hairy</li>
    <li>dog</li>
  </ul>
  <li>enemies</li>
  <ul>
    <li>georgew</li>
    <li>billg</li>
  </ul>
</ol>
1 Like