Creating Blog Statistic/Summary Pages with Jekyll - I
Now that I have the brewlog up and running, I wanted to do more than just make a list of brews. I thought it would be cool to some summarization; total amount of different ingredients used, different styles, and so on.
I wanted to do this within Jekyll without using addtional programs or scripts, and I wanted it utilize the data already available in the posts themselves, instead of creating yet another dataset.
Data
Each post in the brew log has the recipe and brew information defined within the front matter of the post, in YAML.
I originally did this so that the data was accessible from Liquid; I have an include that displays the recipe in a standard format on all posts, and updates to that format only happen in a single file.
It turns out that I can also use it to summarize the posts, too.
Jekyll Liquid Filters
Jekyll already provides some additional Liquid filters that make this task pretty easy, namely where_exp
and group_by_exp
.
Creating a style summary page is actually pretty simple.
First, I group the pages up by style, but only posts for ‘beer’ (later I plan on separating mead and wine out into their own lists):
This:
- takes all of the posts in the site:
site.posts
- filters it down to just ‘beer’ posts:
| where_exp: "item", "item.brew_type == 'beer'"
- groups the remaining posts up by the style:
| group_by_exp: "item", "item.recipe.style"
- assigns it to
beer_styles
:assign beer_styles =
beer_styles
is now an arrary of objects with two attributes:
name
- which is the name of the style (a value frompost.recipe.style
)items
- all of the posts with that value ofpost.recipe.style
Now I can loop through each style, and create a list item for each:
And for each list item (style), group all of the brews by name:
Then, for each brew, display the brew date and link to the post:
The {%if forloop.first <> true %} | {% endif %}
part puts a |
character between each post link. Finally, close up all of the for loops (whew!):
All togther:
A summary page in 14 lines and no custom extenions/scripts…not bad!