【问题标题】:Is it possible to add sub-items to a YML list?是否可以将子项添加到 YML 列表中?
【发布时间】:2013-09-22 17:05:53
【问题描述】:

鉴于以下 YML 格式,是否可以或建议映射 URL,以便可以填充和链接导航列表等组件?

可以填充:

products:
  - Wizzy Widgets
  - Doohickeys
  - Thingamabobbers

通过以下 ERB 呈现(文件为 /data/product_types.yml):

<% data.product_types.products.each do |product_type|  %>
<li><a href="#"><%= product_type %></a></li>
<% end %>

输出以下标记

<li><a href="#">Wizzy Widgets</a></li>
<li><a href="#">Doohickeys</a></li>
<li><a href="#">Thingamabobbers</a></li>

但它也可以链接

products:
  - Wizzy Widgets:
    - url: "/wizzy-widgets"
  - Doohickeys:
    - url: "/doohickeys"
  - Thingamabobbers
    - url: "/thingamabobbers"

像这样通过ERB:

<% data.product_types.products.each do |product_type, product_url|  %>
<li><a href="<%= product_url %>"><%= product_type %></a></li>
<% end %>

以便它输出以下标记?

<li><a href="/wizzy-widgets">Wizzy Widgets</a></li>
<li><a href="/doohickeys">Doohickeys</a></li>
<li><a href="/thingamabobbers">Thingamabobbers</a></li>

知道这个特定的例子不起作用。我只是想举例说明我想要做什么。这是一个不好的做法吗?如果是这样,您将如何处理?

【问题讨论】:

    标签: ruby-on-rails ruby yaml middleman


    【解决方案1】:

    如果您对嵌套 YML 数据感兴趣,可以这样做:

    details:
      - name: "Brady Duncan"
        url: "brady-duncan"
        title: "Secretary of Beer Defense"
        bio: "Has a well rounded set of skills (the right brain) who also aggressively networks and enjoys promoting the brand."
        favorite: "Happy Amber"
      - name: "Jeff Hunt"
        url: "jeff-hunt"
        title: "Beer 'Can'nesseur"
        bio: "Has a very deep understanding of the brewing process and the science behind the 'magic'"
        favorite: "Gnarly Brown"
      - name: "Kenny McNutt"
        url: "kenny-mcnutt"
        title: "The 'Beer'ded Baron"
        bio: "The man with the financial plan who also has a refined pallet for identifying flavors in beer."
        - favorite:
          beer: "Psychopathy"
          music: "Bluegrass"
          movies: "Drama"
    

    【讨论】:

      【解决方案2】:

      试试这个

      require 'yaml'
      
      yml = YAML.load(%{
        products:
          -
            name: Wizzy Widgets
            url: /wizzy-widgets
          -
            name: Doohickeys
            url: /doohickeys
          -
            name: Thingamabobbers
            url: /thingamabobbers
      })
      
      yml["products"].each do |product|
        puts %{<li><a href="#{product["url"]}%>">#{product["name"]}</a></li>}
      end
      

      【讨论】:

      • 还要注意间距在这里很重要。 “-”必须缩进一个空格(不是制表符),后跟一个空格。并且 url 和 name 必须缩进两个空格(也不是制表符)。
      • 您可以在 .yml 文件中执行此操作吗?我应该提一下,这不是典型的 RoR 应用程序。它正在使用中间人。
      • @KevinSuttle Yaml 部分是yml = YAML.load(%{}) 之间的位。它刚刚被放入这样的 Ruby 字符串中,以证明它可以工作。我认为对您来说重要的一点是在哈希中使用 name 键。
      【解决方案3】:

      在 yml 中使用哈希

      products:
        Wizzy Widgets:
          /wizzy-widgets
        Doohickeys:
          /doohickeys
        Thingamabobbers:
          /thingamabobbers
      

      erb 就像你的第二个例子

      <% data.product_types.products.each do |product_type, product_url|  %>
        <li><a href="<%= product_url %>"><%= product_type %></a></li>
      <% end %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2020-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多