Jekyll: Random List

tl;dr: Jekyll provides a sample function which can be used to pick random entries from a given list.

Contents

I wanted to randomize the output of all pages (by default an iterator produces a alphabetically sorted list).

There is no explicit randomize filter for lists available, but Jekyll provides a sample filter1 that can be used:

// Select 1 sample from the list
{% assign foobars = paginator.foobar | sample %}
// Select 10 samples from the list
{% assign foobars = paginator.foobar | sample:10 %}

If you select the range large enough, you get the complete list as randomized list back:

// There are less than 1337 recipes, so we are fine
{% assign foobars = paginator.foobar | sample:1337 %}
{% for foobar in foobars %}
  {% include /components/rfoobar_tile.html foo=foobar %}
{% endfor %}

Footnotes


  1. Jekyll Template Filters - Sample

Tags

Comments

Related