我有一个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
答案 0 :(得分: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>
<强>结果:强>
答案 1 :(得分: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']
}