# YAML inheritance

**URL:** <https://talk.jekyllrb.com/t/yaml-inheritance/1110>\
**Category:** Help\
**Created:** [October 25, 2017, 4:04am UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110 "2017-10-25T04:04:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpl](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jpl/32/575_2.png) [@jpl](https://talk.jekyllrb.com/u/jpl)\
**Post date:** [October 25, 2017, 4:04am UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/1 "2017-10-25T04:04:24Z")

</div>

Hi all,

I was not able to find any information online and I hope someone could share their knowledge.

Is it possible that posts can inherit YAML from the parent? For example, we have this following tree

```auto
en
├── _folder.yml
├── begin
│ ├── _folder.yml
│ ├── onepage.html
│ └── _posts
│ ├── 2000-04-12-exercise-6-sketch.md
│ ├── 2000-04-13-exercise-7.md
│ └── 2000-04-14-exercise-7-sketch.md
└── setup
    └── _posts
        └── 2017-10-23-article.md

```

When

```
en/_folder.yml

```

has a setting

```auto
---
myconfig: "Hello this is my config"
---

```

is it possible that

```
en/begin/_post/*.md

```

will also inherit `myconfig` ?

If there is another `myconfig` in

```
en/begin/_folder.yml

```

`myconfig` will use the value from `en/begin/\_folder.yml

and if `en/begin/_post/*.md` has their own `myconfig` will use their own?

Thanks and appreciate if anyone could share some light. The use case is group configuration.

Cheers  
JP

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [October 25, 2017, 6:58am UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/2 "2017-10-25T06:58:56Z")

</div>

Front Matter **within** a `page` or a `post` is strictly local to that `page` or `post`…  
To configure a group of documents with a single declaration, define them in the `_config.yml`. Jekyll understands such declarations as [`Front Matter Defaults`](https://jekyllrb.com/docs/configuration/#front-matter-defaults)

---

<div class="post-metadata">

**Author:** ![jpl](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jpl/32/575_2.png) [@jpl](https://talk.jekyllrb.com/u/jpl)\
**Post date:** [October 25, 2017, 8:45am UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/3 "2017-10-25T08:45:27Z")

</div>

Thanks @ashmaroli. Appreciate your reply.

Yes, this can be done with `_config.yml` or `_data` folder.

But when using these two methods, if we have multi-level sub-folder, in the \_config.yml we also need to define them multi-level and reading multi-level YAML variable in liquid is really hard as they need to be hardcoded. This makes maintaining the code really hard.

---

<div class="post-metadata">

**Author:** ![jpl](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jpl/32/575_2.png) [@jpl](https://talk.jekyllrb.com/u/jpl)\
**Post date:** [October 25, 2017, 8:58am UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/4 "2017-10-25T08:58:08Z")

</div>

someone also asked about this question here

> <https://stackoverflow.com/questions/23174258/can-yaml-front-matter-be-inherited-in-jekyll>

but no answer.

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [October 25, 2017, 12:41pm UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/5 "2017-10-25T12:41:36Z")

</div>

> [@jpl](#):
>
> we also need to define them multi-level and reading multi-level YAML variable in liquid is really hard as they need to be hardcoded.

I don’t see why you need to define them multi-level… could you illustrate with an example?

---

<div class="post-metadata">

**Author:** ![jpl](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jpl/32/575_2.png) [@jpl](https://talk.jekyllrb.com/u/jpl)\
**Post date:** [October 25, 2017, 11:00pm UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/6 "2017-10-25T23:00:01Z")

</div>

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:

```auto
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:

```auto
---
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**

```auto
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`

```auto
---
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

```auto
---
---
{% 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

```auto
---
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

```auto
---
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

```auto
---
---
{% 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

---

<div class="post-metadata">

**Author:** ![jpl](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jpl/32/575_2.png) [@jpl](https://talk.jekyllrb.com/u/jpl)\
**Post date:** [October 25, 2017, 11:14pm UTC](https://talk.jekyllrb.com/t/yaml-inheritance/1110/7 "2017-10-25T23:14:02Z")

</div>

I think the quickest way to do this is to collect `_folder.yml` along the folder branches and merge them temporary to the post. For example

`book/nonfiction/_folder.yml`

```auto
---
layout: default
article-type: temptype
---

```

`book/nonfiction/science/_folder.yml`

```auto
---
layout: default
article-type: science-temptype
---

```

In the individual post, we combine the `_folder.yml` collected along the folder branches, we have this

```auto
---
layout: default
article-type: temptype
layout: default
article-type: science-temptype
---

```

YAML processor will take the last

```auto
layout: default
article-type: science-temptype

```

as the variables.

For the case of post `2000-01-05-test5.md` after collecting all the ymls, we have

```auto
---
layout: default
article-type: temptype
layout: default
article-type: science-temptype
article-type: individual-type
---

```

and YML processor will take

```auto
layout: default
article-type: individual-type

```

as the variables.

Cheers
