1
votes

I have two Jekyll collections, one references the other via an attribute. Here's a simple example:

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

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

... etc. for md and lg

The collections are exposed to Liquid as arrays, not hashes, so some array walking needs to be done in order to find a given size by its ID. Ideally, I'd like to be able to create a Liquid tag that returns the size document for me to display as needed. In other words, I'd like to do something like this:

{% 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

1 Answers

2
votes

Found this a few minutes after posting: 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