Calling variable within variable inside of YAML front matter block

Hey guys,

This is my first post. I’m new to Jekyll, Git and blogging in general.

I’m trying to create a markdown file that allows me to modify the permalink by using what’s stored inside another variable from the YAML front matter block, here’s an example:

---
layout: post
title: Title Page
custom_slug: my-custom-slug
permalink: blog/{{ page.custom_slug | :title }}/
---

Jekyll will just display what ever text it contains inside the ‘permalink’ variable. I am unable to call a variable inside of the YAML front matter block.

Is there a way to work around this ?

1 Like

Looks like a duplicate of

Liquid is not evaluated in YAML frontmatter. You can try flatify plugin covered in the thread. Or you going to have have to duplicate your text in two values.

If it was some other field and not permalink, I would have said move the logic to a layout which can take a field on the page and rework it into html output

1 Like

Thanks, but I’ve already found a workaround for my particular problem by simply not having the need to use liquid tags inside of the YAML front matter block. Although, it would be nice to know more about whether this was an intentional design feature or not.

I haven’t tested this, but looking at the entry on permalinks in Jekyll docs, the following should be a way to achieve what you’re trying without using Liquid:

---
title: My title
slug: custom-slug
permalink: /:slug/:title
---

As explained in the docs, if you don’t override the slug variable with custom frontmatter as above, by default slug represents the document title slugified.

If you really want to be able to use Liquid in YAML, there is a good post on Stack Overflow here with a couple of workarounds.

1 Like

Yes that will work.

Avoiding setting per post. Set it under config for the posts collection.

Here is a default for all posts.

collections:
  posts:
    permalink: /blog/:name

Name is better than title as name will be lowercase and hyphenated

You might be able to drop the year month day in the output and only use the name bit of the filename.

If you want to use a custom slug for one post, you can use

---
title: My post
permalink: /blog/my-custom-name.html
---

But… note that the typical Jekyll behavior is to use the post filename to set the output url.

2020-01-01-my-post.md
# becomes 
/2020/01/01/my-post.html

And so avoid an anti pattern, i would recommend you update the filename itself in order change the output URL for a given post, rather than only changing the permalink on the post.


If you don’t like the date in the filename then that’s a different issue and already covered in another Jekyll forum post.

And the date required in the filename and output avoids conflicts between posts on different dates with the same name and also allows your RSS feed and site map and posts list to be sorted by date.

See this answer on alias in yaml

1 Like

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)

Your syntax error YAML may be because you have double quotes on the inside and outside so you should do "foo 'bar'" with single quotes for example.

Separately, you cannot use Liquid filters like that in your YAML. It will stay as plain text. You need to put that in the body of your page.

Consider also using an includes file as a function you can call.

_includes/my-images.html

{{ site.images }}{{ page.date | date: "%Y%m%d" }}_

Then use as

---
title: ...
date: ...
---

Here is my caption:

{% include my-images.html %}

Note that page.date will be picked up within the includes.


You can also pass parameters to an includes to make it more flexible.

_includes/my-images.html

{{ includes.image_gallery }}{{ includes.date | date: "%Y%m%d" }}_

Then use as

---
title: ...
date: ...
---

Here is my caption:

{% include my-images.html image_gallery=site.images date=page.date %}

Hi Michael,
Your answer requires a small correction. the “include” parameter data is exposed via variable include instead of includes.