【问题标题】:iterating over yaml data in middleman template在中间人模板中迭代 yaml 数据
【发布时间】:2013-02-26 15:33:47
【问题描述】:

我有一个 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

【问题讨论】:

    标签: ruby sinatra yaml padrino middleman


    【解决方案1】:

    也许您可以在模板中使用完整的projectobject?

    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 公司

    【讨论】:

      【解决方案2】:

      首先您需要将 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']
      }
      

      【讨论】:

      • 也许这是一个技巧问题,yaml 已经加载了data.projects 我只是不确定如何将密钥放入 :locals 哈希中。
      猜你喜欢
      • 2018-04-05
      • 2011-12-14
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多