Using default values for Liquid variables set in _config.yml

I have the following code in my _config.yml file:

# Site defaults
defaults:
      -
        scope:
          path: "" # an empty string here means all files in the project
          type: "posts" # previously `post` in Jekyll 2.2.
        values:
          layout: "default"
          author: "Jonathan Gossage"

and I am trying to use the author variable in my default.html as shown below:

      <body>
        <nav>
          <a href="/">Home</a>
          <a href="/blogs/">Blogs</a>
          <p>Author: {{ author }}</p>
        </nav>
        <footer>
          &copy; {{ site.author }}
        </footer>
      </body>

I have tried to access the default variable as site.author and as author without success. How should I reference default variables? Variables such as page.author are referenced without a problem.

I am having problems rendering the above message properly. The second part of the message contains HTML which gets rendered even when it is formatted as “preformatted text”.

I believe what you have at the top are default page variables not site variables. So any files in the scope should have those page variables available automatically.

If you add author: "someone cool" outside of that defaults block - not indented - then you should be able to access it via site.author.

Do you mean that the defaults block does not apply to site variables? That is not how I read the documentation.

my understanding of the defaults block as you have used it is that it is adding page variables.

If you want a site variable then do that outside of the defaults block.

{{ author }} isn’t going to work with what you have above.

The YAML front matter defaults you’re defining above for type: "posts" are all scoped at the page level. Meaning you’d access author with {{ page.author }} not {{ author }}, which would give you <p>Author: Jonathan Gossage</p>.

1 Like

To summarize answers from @rdyar and @mmistakes,
defaults key is not to set Site defaults per se, but Page / Document Defaults

So, when you have the following,

defaults:
  - scope:
      path: ""
      type: "posts"
    values:
      layout: "default"
      author: "Jonathan Gossage"

it means that all posts will by default use the default.html layout and have “Jonathan Gossage” as the post’s author. If you have a post with no front matter or a post with an empty front matter will have the above values for page.layout and page.author.

To override the default, simply re-define the key in the post’s front matter:

---
author: "John Doe"
---