Open navClose nav

How do I set fallback/hidden values for Front Matter?

Adding Front Matter to your content allows you to add variables and configuration to your pages that can be used in your templates.

Often times, you will find that you are repeating the same front matter values across many files. For example, setting the same layout, or adding the same author across many posts.

Or you may want to provide a default value for optional fields.

In this scenario, it’s possible to provide a fallback or hidden value.

Fallback/Hidden Values in Hugo

To set a fallback or hidden value for front matter in Hugo, you can use the .Param template function.

This will look for the value inside the current page, and if not found, will look for the value in your Site Params, stored in your config file.

For example, setting up a social image:

Inside config.toml

social_image = "/uploads/2018/01/26/og_image.jpg"

Inside content/posts/

social_image = "/uploads/2018/01/31/og_image_example-post.jpg"

Inside layouts/default/single.html

  <meta property="og:image" content="{{ .Param "social_image }}" />

Fallback/Hidden Values in Jekyll

To set a fallback or hidden value for front matter in Hugo, you can use Front Matter Defaults.

This allows you to specify default values for files based on a scope. For example, if you wanted to set a default author for all posts in the _posts collection:

In _config.yml

      path: "" # an empty string here means all files in the project
      type: "posts"
      layout: "post"
      author: "Scott Gallant"

Inside _layouts/post.html:

<div class="author">{{ }}</div> 

Last updated on December 31, 2017