Jekyll: Randomize List

For the recipe index page I wanted to randomize the output of the recipes (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 recipes = paginator.recipes | sample %}
// Select 10 samples from the list
{% assign recipes = paginator.recipes | 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 recipes = paginator.recipes | sample:1337 %}
{% for recipe in recipes %}
  {% include /components/recipe_tile.html recipe=recipe %}
{% endfor %}

Footnotes

Related Posts