Filters
Resources
Reference docs
- Filters in Jekyll docs. See standard filters near the bottom.
- Filters in Shopify docs. Jekyll links to these.
- filters.rb code in Jekyll repo.
Cheatsheets
- Jekyll Cheatsheet on CloudCannon
- Jekyll on DevHints
- gist
Filters
From tutorial
Convert HTML to Markdown
markdownify
e.g.
{{ '# _Heading_ ' | markdownify }}
Convert object to JSON
Convert array or map or a primitive value to JSON value.
jsonify
e.g. foo
with value {"a" => 1, "b" => 2 }
{{ foo | jsonify }}
{"a":1,"b":2}
inspect
Show the value of a variable or expression as an object. This is useful for debugging, especially if a value is hard to find because it is nil or an empty string, this will make it explicity. It will also make sure an array gets rendered as an array and not just space-separated values.
inspect
{{ 'foo' | inspect }}
Try it on some frontmatter values or a data file.
{{ page | inspect }}
{{ site.data.foo | inspect }}
where
where: KEY, VALUE
e.g.
Warning - this will not raise an error if the page is not found.
{% assign item = site.pages | where: 'name', 'foo' | first %}
{% assign posts = site.posts | where: "categories", "foo" %}
{% for post in posts limit: 5 %}
- {{ post.title }}
{% endfor %}
In the above case, using site.categories.foo
would be more convenient.
group_by
{% assign items_grouped = site.posts | group_by: 'author' %}
{{ items_grouped }}
{"name"=>"sharath", "items"=>[#, #], "size"=>2}
{"name"=>"webjeda", "items"=>[#, #, #, #, #, #, #, #, #, #, #, #], "size"=>12}
{"name"=>"someone", "items"=>[#], "size"=>1}
plus
{% assign crumb_limit = forloop.index | plus: 1 %}
Array and loop handling
Sorting
{% assign foo = foo | reverse %}
If the variable is an array of strings:
{% assign foo = foo | sort %}
If the variable is an array of hashes, you need to specify a key to sort on.
{% assign foo = foo | sort: 'path' %}
Size attribute
{% if crumbs.size > 2 %}
{% endif %}
limit
{% for crumb in crumbs limit: crumb_limit %}
{% endfor %}
offset
Do a slice ignoring the last item.
{% for crumb in crumbs offset: 1 %}
{% endfor %}
Similar to pop
except offet
preserves the original value.
{% assign foo = bar | pop %}
Longer form for interest:
{% assign foo = bar | slice: 0, -1 %}
You canโt pop from the front, but you can do a slice starting at 2nd element.
{% assign foo = slice: 1, foo.fize %}
unless, last, index
{% for crumb in crumbs offset: 1 %}
{% unless forloop.first %}
...
{% endunless %}
{% if forloop.last %}
...
{% else %}
{% assign crumb_limit = forloop.index | plus: 1 %}
...
{% endif %}
{% endfor %}