Can I create YAML anchors in _config.yml and call the alias in page frontmatter?

So I understand how to map variables in config.yml that are available globally with {{site.[var]}}. But since we can’t use Liquid in page frontmatter, is it possible to create a YAML anchor in the config file that’s available in my page frontmatter with the corresponding alias? Or possibly using some other method?

For example, I’m using Jekyll to write a book, and there are several pages that need the same title (they’re poems, so just go with me here). I’m using Tom Johnson’s Jekyll theme, so I have YAML files to handle the sidebar & topnav, plus each individual page needs its title. Meaning the title needs to appear maybe a dozen times.

I’d like to do something like this in the config file:
myTitle: &title My reusable title

—and then call it in a page like this:

---
title: *title
author: My Name
layout: page
---

But I can’t figure out a syntax that doesn’t require Liquid to call, if this is even possible.

Any ideas?

YAML anchors/aliases can only be used within the same file, so *title won’t work. One idea is to set the page title via front-matter defaults. For example:

defaults:
  -
    scope:
      path: "" # an empty string here means all files in the project
    values:
      title: "My reusable title"

Pages without a defined title would get the default “My reusable title”. See the Front-matter Defaults docs for more details.

Another alternative is to modify the layout to default to a global title, if a page title isn’t set. For example, the layout would render the page header using liquid like this:

<h1>{{ page.title | default: site.myTitle }}</h1>
1 Like

Related answer

1 Like

Thanks for your response! I didn’t know about the default attribute. I don’t need to use use the Page layout for many pages, so this is a nice solution for me.

1 Like