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.

    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.
# becomes 

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