Skip to content

Instantly share code, notes, and snippets.

@dnozay
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnozay/026862f5d65dcb8b4353 to your computer and use it in GitHub Desktop.
Save dnozay/026862f5d65dcb8b4353 to your computer and use it in GitHub Desktop.
Jekyll filters to help with hashes.
@context = { 'myhash' => { 'b' => 'B', 'a' => 'A', 'c' => 'C' } }
@template = Liquid::Template.parse("{{ myhash | keys | to_ul }}")
@template.render(@context) # => "<ul><li>b</li><li>a</li><li>c</li></ul>"
@template = Liquid::Template.parse("{{ myhash | keys | sort | to_ul }}")
@template.render(@context) # => "<ul><li>a</li><li>b</li><li>c</li></ul>"
module Jekyll
module Toolbox
def keys(hash)
hash.keys
end
def to_ul(collection)
result = ''
result << "<ul>"
collection.each do |item|
result << "<li>#{item}</li>"
end
result << "</ul>"
result
end
end
end
Liquid::Template.register_filter(Jekyll::Toolbox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment