Accessing multi-layered YAML and Middleman

我怕爱的太早我们不能终老 提交于 2019-12-08 03:20:55

问题


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


来源:https://stackoverflow.com/questions/15885392/accessing-multi-layered-yaml-and-middleman

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!