How to use variables from config.yml in frontmatter?

I have a few variables inside my _config.yml file that I’d like to use inside the frontmatter. I tried like this but this is not working:

_config.yml

variable: abc

page.html

---
title: {{ site.variable }}
---

Also, how could I reuse a variable inside the _config.yml file? I tried like this but it is not working too:

variable2: Hey {{ variable }}

Thanks for your answers!

jekyll out of the box doesn’t support that, can’t remember if I have seen a plugin or something that would allow it. I have seen this question before.

Hey @JulSeb42, let me ask: why don’t you use the variable {{site.variables}} just in the content? This is the way I call them from the config file

As a side note, can you share why you want to do this? It is an unusual request because it is not something people typically want to do and there may be a better, “more Jekyll”, way to do it.

I use it also inside the content, but for this case I also needed it in the frontmatter

I’m building a documentation website for a components library at the moment, and we have a list of color names (and other data that we’re reusing in a lot of pages), and each component has most of the content inside the frontmatter. So instead of copying the list of colors every time we need it it’s easier for us to just call it from _config file.

1 Like

Okay, thank you. If I understand you correctly, you might consider creating collections instead. Then, you could build an index page that lists all the colors, like this:

Colors

  • Tangerine href: https://docs/tangerine
  • Fushia href: https://docs/fushia
  • Bubblegum href: https://docs/tangerine

Your _config.yml would include these settings:

collections:
  Red:
    output: true
  Green:
    output: true

Then, you create a folder for each color, like this:

./
└─_Red
│   index.markdown
│   some-doc.markdown
│   some-other-doc.markdown
│   more docs...
└─_Green
│   index.markdown
│   some-doc.markdown
│   some-other-doc.markdown
│   more docs...

You could, of course, set up a permalink to have all your collections in a docs folder, which may be better. I hope this helps!

Here is a link to the official Jekyll documentation on collections:

And here is a link to an article where we go a bit more in-depth here on the forums:

1 Like