module Jekyll::Filters::GroupingFilters
def group_by(input, property)
{"name" => "larry"
Returns an array of Hashes, each looking something like this:
property - the property
input - the inputted Enumerable
Group an array of items by a property
def group_by(input, property) if groupable?(input) groups = input.group_by { |item| item_property(item, property).to_s } grouped_array(groups) else input end end
def group_by_exp(input, variable, expression)
expression -a Liquid comparison expression passed in as a string
variable - the variable to assign each item to in the expression
input - the object array
Group an array of items by an expression
def group_by_exp(input, variable, expression) return input unless groupable?(input) parsed_expr = parse_expression(expression) @context.stack do groups = input.group_by do |item| @context[variable] = item parsed_expr.render(@context) end grouped_array(groups) end end
def groupable?(element)
def groupable?(element) element.respond_to?(:group_by) end
def grouped_array(groups)
def grouped_array(groups) groups.each_with_object([]) do |item, array| array << { "name" => item.first, "items" => item.last, "size" => item.last.size, } end end
def parse_expression(str)
def parse_expression(str) Liquid::Variable.new(str, Liquid::ParseContext.new) end