Check If PublishDate Exists If Not Don't Use Date In Its Place

I would like to check if “publishdate” in my “md” files are not set and NOT use default “date” in its place. Right now if I don’t set “publishdate” it will use “date” instead. I would like to disable this behavior and only show “publishdate” if exists.

Example Markdown:

---
date: "2022-11-11"
title: "Test Page"
---
# My markdown title
my markdown text

So something like this now is WRONG.

{{ with .PublishDate.IsZero }}
  {{ .PublishDate }}
{{ end }}

Result: 2022-11-11
Which used “date” field even when I haven’t set “publishdate” field.

And of course something like this fails with error when there is no “publishdate”.

{{ with .PublishDate }}
  {{ .PublishDate }}
{{ end }}

Error: execute of template failed at <.PublishDate>: can’t evaluate field PublishDate in type time.Time render of “page”.

Which instead should of checked if “publishdate” exists in first place with something like this =(

Maybe introduce something like “IsEmpty” to check if “publishdate” even exists. :slight_smile:

Thanks for any help!

A small workaround by using seperate “history” with dates in markdown files. As per previous solved question @ Get First Date From History Param

Example Markdown File:

history:
  - "2022-03-10"
  - "2022-02-05"
  - "2022-01-01"
{{ with .Params.history }}
  {{/* Get first date in the history list. */}}
  Updated: {{ range first 1 . }} {{ . }} {{ end }}
  {{/* Get last date in the history list. */}}
  Created: {{ range last 1 . }} {{ . }} {{ end }}
{{ end }}

If some one has a better solution I’m all ears :slight_smile:

So you don’t want publishDate to show the date? I’m confused! What are you trying to achieve exactly?

Right now date is used when publishdate does not exist. I would like to disable this behaviour.

Example Markdown:

---
date: 2022-11-11
title: "About"
---
some text here

How it works now:

{{ with .PublishDate }}
  {{ .PublishDate }}
{{ end }}
Result: 2022-11-11

It gets date field by default even when publishdate is not set in md file.

site config

[frontmatter]
date = ['date']
publishDate = ['publishDate']

template

{{ with .Date }}
  Date: {{ . | time.Format ":date_long" }}
{{ end }}

{{ with .PublishDate }}
  Publish date: {{ . | time.Format ":date_long" }}
{{ end }}

https://gohugo.io/getting-started/configuration/#configure-front-matter

1 Like

Hey jmooring, I actually just figured this out on my own before I checked the forum here. But thank you :slight_smile: Here is how I tried it.

config.yaml

frontmatter:
  publishDate: [ "publishDate" ]

in single.html

{{ if .PublishDate }}
  {{ .PublishDate }}
{{ end }}

-OR-

{{ if isset .Params "publishdate" }}
  {{ .PublishDate }}
{{ end }}

The only thing with my solution I can’t seem to use with for some reason.

For example something like this trows error.

{{ with .PublishDate }}
  {{ .PublishDate }}
{{ end }}

Error: execute of template failed at <.PublishDate>: can’t evaluate field PublishDate in type time.Time render of “page” failed:

No.

The with action rebinds the context (the dot) to its argument. A simplified version of my example (no date formatting) is:

{{ with .PublishDate }}
  {{ . }}
{{ end }}
1 Like

Yes you are right :slight_smile: Tested and it works. Thanks!

EDIT: And if some one else wonders how to use Format inside with :slight_smile:

{{ with .PublishDate }}
  {{ .Format "Jan 2, 2006" }}
{{ end }}

-OR- If you prefer using string, something like this.

{{ with .PublishDate }}
  {{ $published := .Format "Jan 2, 2006" }}
  {{ $published }}
{{ end }}

But with time.Format it will be localized for each language in your site.

{{ with .Date }}
  Date: {{ . | time.Format ":date_long" }}
{{ end }}
1 Like

Okay one more problem with Format :slight_smile: How do you show days like 1st, 2nd, 3rd, 4th?

This is what I’m using now.

{{ with .PublishDate }}
  {{ .Format "Jan 2, 2006" }}
{{ end }}

Wanted Result:

Nov 1st, 2022
{{ with .PublishDate }}
  {{ printf "%s %s, %s" (.Format "Jan") (.Format "2" | humanize) (.Format "2006") }}
{{ end }}
1 Like

@jmooring Thank you, seems a bit too long for something simple like this :slight_smile: Maybe this should be added into Hugo Format one day. Something like {{ .Format "Jan 1st, 2006" }} where st is for prefix’s like “st”, “nd”, “rd”, “th”.

Here is a bit cleaner version without using printf :slight_smile:

{{ with .Date }}
  {{ $month := .Format "Jan" }}
  {{ $days := .Format "2" | humanize }}
  {{ $year := .Format "2006" }}
  {{ $month }} {{ $days }}, {{ $year }}
{{ end }}

The formatting is provided by Go’s time package.

And hey, if you want to write four lines of code and initialize three new variables, instead of writing 1 line of code with no new variables, knock yourself out…

1 Like

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