2014-10-08 75 views
1

我有兩個Jekyll集合,一個通過屬性引用另一個集合。下面是一個簡單的例子:在Jekyll中定製液體標籤以查找屬性的集合文檔

--- 
# product 
id: my-awesome-product 
name: My Awesome Product 
sizes: 
    - sm 
    - md 
    - lg 
--- 

--- 
# size 
id: sm 
price: $10.00 
--- 

... etc. for md and lg 

的集合被暴露於液體如陣列,不哈希,所以一些陣列行走需要,以便找到由它的ID給定的大小來進行。理想情況下,我希望能夠創建一個Liquid標籤,它返回尺寸文檔以供我根據需要進行顯示。換句話說,我希望做這樣的事情:

{% for product in site.products %} 
    {{ product.name }} 

    {% for s in product.sizes %} 
    {% size s %} # how do I create a Liquid Tag that starts like this, 
     * {{ size.id }} ({{ size.price }}) # looks up and grants access to a size 
    {% endsize %} # and ends liks this 
    {% endfor %} 
{% endfor %} 

回答

1

發佈後這才發現幾分鐘:https://gist.github.com/danielcooper/3118852#file-rss_tag-rb

module Jekyll 
    class Size < Liquid::Block 
    def render(context) 
     context.stack do 
     context['size'] = context.registers[:site].collections['sizes'].docs.find { |size| 
      size.data['id'] == context[@markup.strip] 
     } 
     render_all(@nodelist, context) 
     end 
    end 
    end 

    Liquid::Template.register_tag('size', Size) 
end