我有两个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
集合以数组的形式暴露给Liquid,而不是哈希,因此需要进行一些数组遍历才能通过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 %}
答案 0 :(得分: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