When I recently rewrote my site using Jekyll, one thing I really missed was having something like Rails’
content_for mechanism. Using
content_for, a page can inform its parent layout about content that needs to be put in various places on the rendered page. A couple of popular examples are:
I saw a few implementations out there of this kind of functionality for Jekyll, but some were too specific (i.e. sidebar only), others didn’t seem to work with the latest version of Jekyll, and still others worked but did not allow for Markdown within the blocks.
Thus was born the jekyll-contentblocks plugin.
The plugin introduces two new Liquid tags:
contentfor. Here’s an example of how they are used. Say we have a layout,
This layout defines two places where a page can add content:
Now let’s say we want to include a specific set of CSS styles specific to posts. To do that, we define our
_layouts/post.html layout like this:
contentfor styles Liquid block like above would pull the
posts.css file into every page using the
post layout. Now, if we have a post that wants to include some content in the sidebar, we can simply create a file like
As you can see, the
contentfor blocks can render Markdown (or whatever the current file format is) and they will render Liquid tags within them.
So that’s basically it – please give the plugin a try in your own Jekyll project and let me know on the plugin’s GitHub page if you encounter any issues!