A light copy-and-paste Jekyll plugin to get keys from a hash

Jekyll Keys Filter

A light copy-and-paste Jekyll plugin to get keys from a hash

This a great example of using Ruby code in just a single script to create a simple Jekyll plugin.

How to use gem like this:

  • Package as a Ruby gem, with a gemspec file.
    • Publish to RubyGems for installation using gem or bundle commands.
    • Provide instructions for installing your gem using a GitHub URL instead, using the same install commands.
  • Copy the Ruby script directly to your Jekyll plugins directory - _plugins. Then use in your Jekyll code, or run with:
    $ bundle exec ruby _plugins/script.rb

Note that regardless of which flow you use, custom gems outside of the standard are considered unsafe and unsupported on GitHub Pages.

Sample usage

Jekyll templating example using a hash variable.

{% assign my_hash =  {'foo': 'bar', 'baz': 'buzz'} %}

{{ my_hash | keys | join: ', ' }}

Rendered content:

foo, baz


In your Jekyll project, create a file as _plugins/keys-filter.rb.

Copy the contents of keys-filter.rb to it.

That plugin uses standard Ruby functionality of getting keys from a hash - it makes that available as a Jekyll filter called keys, as below.


{{ my_hash | keys }}

The value will be the keys as an array of strings.

[ "foo", "bar", "bazz" ]



Released under ISC.

Copyright for keys-filter.rb is held by @dalenys. The early commits here are by @nemenemsrouge.

All other copyright is held by @MichaelCurrin.

I forked this project so I could keep a copy of the plugin and improve its docs. The original repo was taken off of GitHub.

jekyll logo

Want a Jekyll website built?

Hire a Jekyll developer

Get New Themes & Resources