public
Last active

Quick'n'dirty Jekyll plugin for sorted cycle

  • Download Gist
README.md
Markdown

Jekyll sorted_for plugin

Quick'n'dirty Jekyll plugin for sorted cycle.

Install

Copy sorted_for.rb to _plugins/ directory of your Jekyll site.

Usage

Instead of for in templates use sorted_for and add sort_by parameter with property you want to sort by. Also supports reversed parameter as the original for tag.

{% sorted_for node in site.pages reversed sort_by:weight %}
  {{ node.title }}
{% endsorted_for %}

To use custom sort property (eg. weight as in example above) add it to YAML Front Matter of your pages - see https://github.com/mojombo/jekyll/wiki/YAML-Front-Matter

sorted_for.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
module Jekyll
class SortedForTag < Liquid::For
def render(context)
sorted_collection = context[@collection_name].dup
sorted_collection.sort_by! { |i| i.to_liquid[@attributes['sort_by']] }
 
sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
context[sorted_collection_name] = sorted_collection
@collection_name = sorted_collection_name
 
super
end
 
def end_tag
'endsorted_for'
end
end
end
 
Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)

Forked this as https://gist.github.com/3812259 and added a few changes:

  • Add sorted_for_test
  • Allow sorted_for to handle arrays of primitives
  • Add sorted_keys_for tag to iterate over hash keys

Forked also as https://gist.github.com/cloudshark/5313141 because I needed to gsub the sorted_collection_name in my case where I had more than one dot already in the collection name

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.