Last active
August 29, 2015 14:08
-
-
Save dnozay/026862f5d65dcb8b4353 to your computer and use it in GitHub Desktop.
Jekyll filters to help with hashes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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