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:
gem
or bundle
commands. _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.
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.