I have a Jekyll app (using Liquid) and I’d like to know how to, in Liquid, group a collection of items into a small subset of collections.
For instance, pretend I have this array:
fruits = ['apples', 'oranges', 'bananas', 'pears', 'grapes']
What I’d really like to do, in the Liquid page, is get this:
fruit_groups = [['apples', 'oranges'], ['bananas', 'pears'], ['grapes', null]]
For example, Ruby on Rails can do this with their .group_by method attached to enumerables.
Can I do this in Liquid?
Use case: I have a big collection of items, but I need to convert them into columns of <ul> elements. So, if I have three columns, I need to get three sub-collections.
Thanks!
This doesn’t answer your question directly, but I’m not sure that you’d want to do what you’re describing (and I’m pretty sure you can’t) – Liquid is a templating system, not a fully fledged programming language. I suspect that you’ll be able to achieve your end goal using some of the for loop and cycle features: http://code.google.com/p/liquid-markup/wiki/UsingLiquidTemplates
eg:
or
If you have three columns, you’d just have three steps in your cycle, and three directions to go in your if statement.
If all that fails, you could write a plugin (in Ruby) that will restructure your data before it ever hits the template layer, but I suspect that would be overkill.