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.
{{ 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 }}
@jmooring Thank you, seems a bit too long for something simple like this 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
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…