< Useful Jekyll snippets

While I have worked on my own Jekyll website, I have come across some useful snippets that I thought was too good to not share especially since they were not that easy to find unless you knew where to look.

Body class for pages based on page title

I actually found this via an issue on GitHub and thought it was nifty if you wanted to have specific styling for each page, a bit like WordPress does.

<body{% if page.slug %} class="{{ page.slug }}"{% endif %}>

If your page title was about, then this would insert the page title variable into the body class like below:

<body class="about">

This is a relatively simple snippet which would allow you to have different pages with their own styling/layouts which would make a website certainly more interesting.


There are many more useful snippets out on the Internet for Jekyll, and I have found that places like GitHub or Stack Overflow can be quite useful in finding out what you would like to do whenever it's to have a certain functionality or something to be changed from the default Jekyll install. Here's a list of places where I check for Jekyll snippets or solving problems.

  1. Jekyll Snippets on GitHub.
  2. Newest 'Jekyll' Questions on Stack Overflow.

If you know of any other places that have things that involve Jekyll and code snippets, then please share them with me via @j_mes.