Jekyll Collections

Figuring out collections with Jekyll is kind of like Custom Post Types.

Early on, Jekyll’s developers said that if someone was using posts for non-blog content, they were doing something wrong. That left one other avenue open, the first time I looked at Jekyll, which was pages. They’re nice, but they’re not what I wanted.

Enter Jekyll collections. These are ‘arbitrary’ groups of related content which you put in their own folder. I had 15 years of interviews collected, so for me this seemed like a perfect idea. I read up on Ben Balter’s – Explain Jekyll Collections like I’m 5 and it helped me sort out what I wanted.

Configure

This is easy. You just add the collection code to _config.yaml

# Collections
collections:
  interviews:
    output: true

Having the output set to true means that when I run jekyll build the pages are generated. That’s pretty simple. They don’t get auto-generated when you run a jekyll serve and you’re testing locally, however. Which sucks. I upgraded to Jekyll 3.0 beta and it started working, though, and I’m okay with running a beta.

Create A Folder

Also easy. Make a folder called _interviews in the main Jekyll folder. I will note, this gave me a fit. I wish I could put all my collections in a subfolder, because now I have this:

_data
_includes
_interviews
_layouts
_pages
_posts
_sass
_site

It’s messy, and if I didn’t know that some of those folders are special (like _includes) I could easily be confused. The _site folder makes some sense, that’s where my site is output. But even if I use the source setting to move all my source pages into a folder (called _source in my case), I still can’t separate the code from the content. What I would like is this:

_assets – Store all of my ‘code’ like layouts, plugins, css, etc here.
_content – Store all my post content, collections, pages, etc here.

Still this is a little better for me. Less insane. I will note, I was able to move my folders by defining the directories in my configuration file like this:

# Moving Folders
source:       _content
plugins_dir:  _jekyll/plugins
layouts_dir:  _jekyll/layouts
includes_dir: _jekyll/includes

So now my main folder has two folders _site and _content which is a lot easier for me to work with. I feel less muddled. Inside the content folder is a _jekyll folder which is my ‘wp-content’ folder, and a _data folder, which has some data files. More on that later.

NB: This only works on Jekyll 3.0 and up!

Create Files

All I had to do was make my files in my _interviews folder and I was done. Well. Not really. I needed a way for Jekyll to link through everything, and I really didn’t think making manual pages was smart. I tossed in this code to my interviews post file and it cleverly looped through everything it found, generating the page on the fly:

<ul>
{% for topic in site.interviews %}
	<li><a href="{{ site.baseurl }}{{ topic.url }}">{{ topic.title }}</a></li>
{% endfor %}
</ul>

If you’re familiar with WordPress loops, this is the same thing as saying “For all posts in a category…”

Customize the Hell Out of It

Of course you know that’s what I did next. I went and made it super-complex by putting my interviews in year subfolders and then making the main interview page a list of all the years, with links to those pages, and loops back and … well. That’s another post.

%d bloggers like this: