How do you make Jekyll execute Liquid variables in front matter


#1

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

Pretty sure liquid is not allowed in the front matter.


#3

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…


#4

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:


#5

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…


#6

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.


#7

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!


#8

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.


#9

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


#10

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!