Given the following YML format, is it possible, or advisable to map URLs so that components like nav lists can be both populated and linked?

It can be populated:

products:
  - Wizzy Widgets
  - Doohickeys
  - Thingamabobbers

renders via the following ERB (where the file is /data/product_types.yml):

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

to output the following markup

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

but can it be linked also

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

through ERB like so:

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

so that it outputs the following markup?

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

I know this particular example doesn't work. I'm just trying to give an example of what I'm looking to do. Is this a bad practice? If so, how would you approach it?

有帮助吗?

解决方案

If you're interested in nesting YML data you can do so like this:

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"

其他提示

Try this

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

use a hash in yml

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

erb like your second example

<% data.product_types.products.each do |product_type, product_url|  %>
  <li><a href="<%= product_url %>"><%= product_type %></a></li>
<% end %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top