Using `:filename` in front matter

Continuing the discussion from Set date in front matter from `hugo new` command:

Are there any other steps to having this work aside from adding the following to config.yaml?

frontmatter:
  date:
  - :filename
  - :default

My archetypes/journal.md is:

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
---

If I do hugo new journal/2018-02-22-mypage.md I get:

---
title: "2018 02 22 Mypage"
date: 2022-11-07T17:57:51-08:00
---

What am I missing? :thinking:

Yes, but when you render {{ .Date }} it gets it from the filename. The date value in your markdown is a fallback.

Or to put it another way, the date in your front matter is superfluous, irrelevant, and redundant in this configuration.

1 Like

Oh, okay! So I can just leave date: out of my frontmatter, as long as I include it in the filename?

Yes.

If you want to grab the date from the filename, and stuff it into the date in front matter when you create the file, do this in your archetype:

date = {{ .File.ContentBaseName | replaceRE `(\d{4}-\d{2}-\d{2}).*` "$1" }}

But that doesn’t add any value unless you later remove the date from the filename.

5 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.