I like the beautiful part of how Jekyll take care of categories using folders, and be able to do it muti-level. Lets take this as an example, assuming we have 1000 files in book/nonfiction/science/biology/_posts
in the following order:
book
└── nonfiction
├── maths
│ ├── stats
│ │ └── _posts
│ └── trig
│ └── _posts
└── science
└── biology
└── _posts
├── 2000-01-01-test.md
├── 2000-01-02-test2.md
├── 2000-01-03-test3.md
├── 2000-01-04-test4.md
└── 2000-01-05-test5.md
In each of the post in book/nonfiction/sciense/biology/_posts
it can access this variable
{{ post.categories | split: ',' }}
and returns
[“book” “nonfiction” “science” “biology”]
This means if we decided to move 200 books in book/nonfiction/sciense/biology/_posts
to book/nonfiction/sciense/biology/ecology/_posts
we can just move them there and
{{ post.categories | split: ',' }}
will magically returns
[“book” “nonfiction” “science” “biology” “ecology”]
We don’t need to edit 200 files and add ecology
category to their front matter. post.categories
will automatically inherit the categories along the folders. This is the truely beautiful part of Jekyll categories.
Now lets look at how front matter inheritance can add synergy to the folder based categories feature in Jekyll.
Currently, each post will need to have their own front matter, for example:
---
layout: book
article-type: rectangle
----
{% if page.article-type %}
* this article is type {{ page.article-type }}
{% endif %}
All 1000 files will have the above front matter.
Scenario 1 - Change the article-type
to circle
for all
We will need to edit all 1000 files to change rectangle
to circle
Solution 1 - Use _data
(_config.yml
should be similar to this)
book.yml
nonfiction:
science:
biology:
article-type: circle
chemistry:
article-type: quad
maths:
stats:
article-type: triangle
trig:
article-type: sphere
In the 1000 posts we have this, looks simpler when using _data
---
layout: default
---
{% if site.data.book.nonfiction.science.biology.article-type %}
* this article is type {{ site.data.book.nonfiction.science.biology.article-type }}
{% endif %}
Now this is perfect if the posts stays the same. Lets say due to “temporary” business decision, we want all nonfiction
posts to be article-type: temptype
to reflect a temporary promotion or temporary status.
We either need to go through all the 1000 files and add article-type: temptype
into the them or we edit each individual posts to change the hard-coded site.data.book.nonfiction.science.biology.article-type
liquid code to site.data.book.nonfiction.article-type
instead. Which ever way it is, it is tedious.
Proposed Solution using folder based YAML inheritance
In the 1000 posts we have this
---
---
{% if page.article-type %}
* this article is type {{ page.article-type }}
{% endif %}
Note that there is no article-type
and no layout: default
in the post.
Logic 1
In folder book/nonfiction/
we add a YML file, lets just call it _folder.yml
Inside the _folder.yml
we have this
---
layout: default
article-type: temptype
---
If there is YML inheritance, any posts down the folder `book/nonfiction’ will inherit these two variables. Therefore, by just adding or editing 1 file, we can change the behavior of 1000 files down folder branches.
Now, lets say, there is this particular post 2000-01-05-test5.md
that is not affected by the temporary business decision, and it need to have its own article-type
, we just need to edit this file to have
---
article-type: individual-type
---
of its own.
Therefore, 999 files inherited the variables of _folder.yml
from their parent, and ` file has its own variable.
Now, lets say, there is another change that only affects book/nonfiction/science
, we just need to add _folder.yml
inside that folder and all files down the folder branch will inherit the variables in book/nonfiction/science/_folder.yml
and over-ride the parent’s variables. This change also does not affect other folders like book/nonfiction/maths
that has the inheritance from book/nonfiction/_folder.yml
And the most beautiful part is in the 1000 posts, we just need to have this
---
---
{% if page.article-type %}
* this article is type {{ page.article-type }}
{% endif %}
They all use the same variable name page.article-type
, and the liquid code is not affected by any changes done. We can move the posts anywhere, any folder and still the rules follows the parent’s _folder.yml
.
I think the proposed solution can be achieved by plugins, but ideally if it is inside the core of Jekyll, it will help lots of uses hosting on github.
Sorry for such a long-winded reply, this is just a suggestion and I do believe it will make Jekyll more beautiful and powerful.
Cheers