0

我有一个 yaml 文件中的项目列表,我想动态循环通过中间人应用程序中的模板,但我不确定如何或是否可以/应该更动态地执行它。

我有:

data.projects.each do |f|
  proxy "/work/#{f[:name].parameterize}.html", "/work/template.html", 
    :locals => { name: f[:name], client: f[:client], ... } #would like to dynamically pull in keys as locals.
end

ignore "/work/template.html"

yaml:

-  name: Acme Website
   client: Acme Inc
   overview: "Cupcake ipsum dolor sit amet wafer gummi bears pudding applicake. Jujubes brownie powder. Sweet roll powder gingerbread gummies. Cupcake ice cream sweet roll pie lollipop. Jelly-o jelly-o apple pie chupa chups jelly jujubes gingerbread. Icing carrot cake powder chupa chups. Pudding sweet roll jelly-o muffin faworki biscuit. Marzipan marshmallow cake tiramisu caramels bear claw carrot cake cotton candy. Toffee danish sweet roll. Cookie topping powder toffee ice cream muffin dragée. Soufflé caramels apple pie chocolate cake cookie cake. Macaroon tiramisu halvah soufflé. Dessert toffee halvah chocolate cake bear claw."
   skills:
   - design
   - development
   - branding
   - ecommerce
   stack:
   - middleman
   - branding
   - design
4

2 回答 2

1

也许您可以project在模板中使用完整的对象?

data.projects.each do |p|
  proxy "/work/#{p[:name].parameterize}.html", "work/template.html",
    :locals => { :p => p }
end

...应该让您在以下范围内访问您的项目http://0.0.0.0:4567/work/acme-website.html

<ul>
 <li><%= p.name %></li>
 <li><%= p.client %></li>
</ul>

结果:

  • Acme 网站
  • Acme 公司
于 2013-06-27T06:57:06.190 回答
0

首先,您需要将 YAML 解析回 Ruby 对象,然后从那里开始:

require 'yaml'

doc = YAML.load(<<EOT)
---
-  name: Acme Website
  client: Acme Inc
  overview: "Cupcake ipsum dolor sit amet wafer gummi bears pudding applicake. Jujubes brownie powder. Sweet roll powder gingerbread gummies. Cupcake ice cream sweet roll pie lollipop. Jelly-o jelly-o apple pie chupa chups jelly jujubes gingerbread. Icing carrot cake powder chupa chups. Pudding sweet roll jelly-o muffin faworki biscuit. Marzipan marshmallow cake tiramisu caramels bear claw carrot cake cotton candy. Toffee danish sweet roll. Cookie topping powder toffee ice cream muffin dragée. Soufflé caramels apple pie chocolate cake cookie cake. Macaroon tiramisu halvah soufflé. Dessert toffee halvah chocolate cake bear claw."
  skills:
  - design
  - development
  - branding
  - ecommerce
  stack:
  - middleman
  - branding
  - design
EOT

require 'pp'
pp doc

哪个输出:

[{"name"=>"Acme Website",
  "client"=>"Acme Inc",
  "overview"=>
  "Cupcake ipsum dolor sit amet wafer gummi bears pudding applicake. Jujubes brownie powder. Sweet roll powder gingerbread gummies. Cupcake ice cream sweet roll pie lollipop. Jelly-o jelly-o apple pie chupa chups jelly jujubes gingerbread. Icing carrot cake powder chupa chups. Pudding sweet roll jelly-o muffin faworki biscuit. Marzipan marshmallow cake tiramisu caramels bear claw carrot cake cotton candy. Toffee danish sweet roll. Cookie topping powder toffee ice cream muffin dragée. Soufflé caramels apple pie chocolate cake cookie cake. Macaroon tiramisu halvah soufflé. Dessert toffee halvah chocolate cake bear claw.",
  "skills"=>["design", "development", "branding", "ecommerce"],
  "stack"=>["middleman", "branding", "design"]}]

这是一个哈希数组。您可以遍历返回的数组,并提取嵌入的哈希值并正常处理它们:

doc.each { |h|
  puts h['name']
  puts h['client']
}
于 2013-02-26T15:43:27.387 回答