Jekyll Collections Versus Posts

Published:
Last modified:
Tags Jekyll , Collections

Differences

These are the main differences between Collections and posts.

Categories

A post in a subdirectory structure automatically gets subdirectories names as its categories.

A collection does not have categories by default.

_Categories_ can be assigned to _collections_ with a parameter in each file _front matter_.

Filenames

Post filenames must be in the format YYYY-MM-DD-title.<markup> which assigns the post date, used to sort posts by creation date.

Collection filenames doesn’t need any special format in filenames, they get the date from the file creation date.

In both cases, the document's date can be overwritten in the front matter of each file setting the date variable. (i.e. date: 2016-06-08)

Directories

Posts have the restriction of being only recognized if they are located under a _posts folder. This restriction won’t be removed as it is being kept to not break previous versions.

Collections can be located directly in any directory of the collection folder.

Dates

This is one of the most important difference between them.

Posts were originally aimed for blogs, so the date is a central concept that is also displayed in its filename, they are not likely to get updated after published.

└── _posts
   β”œβ”€β”€ 2016-11-19-why-every-developer-should-use-emacs.md
   └── 2016-06-20-open-source-in-governments.md
  • Beside collections have a date property, this is not an important concept as it is in posts, a clear structure is needed to locate each file quickly when needed to update or add data to them.
└── _my_collection
   β”œβ”€β”€ open-source-in-governments.md
   └── why-every-developer-should-use-emacs.md

Summary

Considering publishing a list of books with both approaches:

  • a list of standard posts in books folder.

  • a collection in _books folder, with the following configuration:

    • In /_config.yml:

        collections:
          books:
            output: true
      
Note the underscore as the first character of the collection folder /_books, needed to let Jekyll handle the directory as a collection.

This is how they basically compare:

#CollectionPost
Directories/_books/science//books/science/_posts/
Fileitem.md2016-06-10-item.md
{{ page.categories }}["books", "science"]
{{ page.date }}from file creation date:
2016-07-11 23:03:59 |
from filename: 2016-06-10
Document accesssite.books
site.collections
site.categories.science,
site.collections,
site.categories.books
or
site.posts |
File handling the URLhttp://example.com/books/science/books.html with permalink: "/books/science"
or
/books/science/index.md

Choosing between Collections and Posts

Features of Collections:

  • a cleaner directory/subdirectories structure without _posts folder in each nested level
  • cleaner filenames without mandatory dates.
  • In a hierarchical subdirectoy structure, categories can be simulated processing the path property of each file.
  • Collection filenames put more emphasis in its title, not the date.

Features of Posts:

  • Posts were introduced in Jekyll before collections, so they are widely adopted and with more plugins available.
  • Posts filenames put more emphasis in dates, not the titles.
  • when using nested directories, creating each subdirectory index is straightforward.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·