Accessing multi-layered YAML and Middleman

2019-05-30 17:25发布

问题:

After taking a look at these two links, using YAML with Middleman has become a lot more clear: Middleman Docs(Local Data), parsing and composing YAML

The issue that I'm running into now is accessing multiple levels of content.

YAML (lives in data/projects)

- quote: This is a quote
  attribution: Kate Something
  extras:
      - extra one
      - extra two
      - extra three

- quote: Blah blah
  attribution: Donna Doe
  extras:
      - another extra
      - another extra

.HTML.ERB

<% data.projects.each do |f| %>
    <div><%= f["quote"] %>  <%= f["attribution"] %> <%= f["extras"] %></div> 
<% end %>

The above is running smoothly with Middleman, however, how can I access the data underneath "extras:" and spit them out in a list?

In other words, this is what is compiled in build:

<div>This is a quote  Kate Something extra oneextra twoextra three</div>

This is the result that needs to be achieved:

<div>This is a quote  Kate Something 
  <ul>
    <li>extra one</li>
    <li>extra two</li>
    <li>extra three</li>
  </ul>
</div>

Thank you in advance for taking a look at this issues. Please let me know if you need clarification on any of the above, and I'll try to explain further.

回答1:

f["extras"] is just another array, so you can iterate over it the same way you iterate over data.projects:

<% data.projects.each do |f| %>
    <div><%= f["quote"] %>  <%= f["attribution"] %>
      <ul>
        <% f["extras"].each do |extra| %> <%# inner loop here %>
          <li><%= extra %></li>
        <% end %>
      </ul>
    </div> 
<% end %>