How do you make Jekyll execute Liquid variables in front matter

Hi, I’m trying to call Liquid values into my layout/include front matter. But usually Jekyll outputs it raw without executing it.

Say I got

---
title: Welcome to {{ site.title }}
---

I’d expect the output to be “Welcome to My Website”; instead I get “Welcome to {{ site.title }}”, meaning the Liquid tag isn’t executed.

Is there a way to get this done?

Thanks.

2 Likes

Pretty sure liquid is not allowed in the front matter.

So, I wonder is this a bug or a feature…and I’m really curious to find a way around it because it’s increasingly becoming a need for my use cases…it’s why I posted it here in such detail…

I think it is intended behavior based on the order of how things are render would be my guess.

Your example is very simple, is there something else you are trying to accomplish? for example, if you are using title for the Title in the Head, then you can use an include and use liquid there to insert the site.title.

First answer here has a clever solution that may work:

Thanks, @rdyar, so this Liquid’s replace filter method did work in exactly the same way as used, with !SITE.URL!. I supposed I can try it with other variables, too. But I just wish there was a way to get around it automatically…

I mean, at ease, I’d love to just drop variable calls/placeholders even while in front matter…

yeah, that was hacky but outside the box thinking.

I’m sure others would like the same thing, but I’m guessing it is not easy to accomplish.

While Liquid isn’t allowed in Frontmatter out of the box, there’s a fairly simple plugin you can use to get around this. This blog post contains examples and the code you need, but all you need to do is add a Liquid filter to your variables to output the actual variables of the thing you’re referencing.

For your example, this won’t work to render the actual title (the one displayed as the title of a tab in a browser, etc), but it will work for rendering content on the pages themselves. For example:

_config.yml:

title: "My Website"


index.html:
---
title: "Welcome to {{ site.title }}"
---

{{ page.title | flatify }}

This would output Welcome to My Website.

It isn’t perfect, but I’ve found this to be really helpful overall!

1 Like

Thanks, @erinkcochran I’ll take a look at the post, implement the suggested plugin and hope it works…is this something Jekyll could consider making possible within its core in future?

Thanks.

O, my God, finally a solution that worked, nifty! Thanks very much @erinkcochran

You’re welcome! It’d be super if the Jekyll team would implement it within the core. An issue was recently opened on the repo that asks for this to be a core feature - go pile on!

2 Likes

@erinkcochran

This not work for me. I’m using jekyll 3.8.2. Any suggestions? Tks.

Interesting. Why not, though, can you share snippets of your code, perhaps where you feel the issue may be coming from? I’d be glad to take a look.

@kblife @erinkcochran
Sorry, this work for me too! I forgot “| flatify”

1 Like

@bmr It looks like your post was withdrawn - did you manage to figure out the issue? I’m happy to help if I can!

PLEASE HELP
i have a front matter:

---
sjcm: "{{ site.data.para1.para2 }}"
---

actually that is a json and i want to access that json so:
{{ page.sjcm | flatify }}

this gives me sth like:

{“a”: “b”,“c”:“d”}{“e”:“f”,“g”:“h”}

but i cant iterate on it due to lack of "," because what i want and what really is in file is:

{“a”: “b”,“c”:“d”},{“e”:“f”,“g”:“h”}

tnx for help

one thing at a time… you can’t use liquid in the front matter by default, above they are using a plugin. Are you using that?

as for the json issue, if you think the liquid filter is doing something wrong I would make a new post just for that with a minimal example of how to test it.

tnx rdyar, yes i’m using that plugin and its very useful but not in my case!
actually that plugin is useful when you want to move an stringed-variable to frontmatter, but what i want is different , i want to use a specific json file to iterate on and automatically make a post layout with that which it’s address is site.data.para1.para2 and it should be given in frontmatter but the json i get from front matter seems stringifed and its not iterable!

Hi, I am new to jekyll but I quickly ran into the desire to have liquid variable in the front matter and I stumbles on this which worked for the title and other variable. However I would like o flatify the date and it’s giving me an error I am presuming because of the % sign in column 48 of line 4. I am not sure if I need escape the quotes or % sign or how to escape them (I tried to escape with \ and it still gave me an error)

---
layout: post
title: Test
caption: "{{site.images}}{{ page.date | date: "%Y%m%d" }}_"
---

<h1>{{ page.caption | flatify }}</h1>

error:

          Error: YAML Exception reading /home/test/Documents/websites/websitename/_posts/2022-05-26-1.md: (<unknown>): found character that cannot start any token while scanning for the next token at line 4 column 48

Some background about what I am trying to accomplish. Instead of naming my post I am just giving them a name like 2022-05-26-1.md and then name them using the title: in the front matter. If I need to attach an image I just put the image in the /assets/img folder with the name of the date 20220526_1.jpg. Then in the text where I need to use the image I just write ![img]({{ page.caption | flatify}}_1.jpg)