Show content if date is set in frontmatter

I want each page to show its creation date, but only if I have set a date in the front matter.

My content file has:

date: “2015-08-15”

And in my _default/single.html template:

{{ if eq .Type “page” }}
{{ if isset .Date }}
{{ .Date.Format “Monday, Jan 2, 2006” }}
{{ end }}
{{ end }}

Apparently {{ if isset .Date }} is not the correct syntax. What should I use instead?

Date is always set. May be better to say after a certain date.

Try:

   {{ if not .Date.IsZero }}

Date is never nil – but it has a zero value if not set.

2 Likes

Thanks! That works perfectly, and it’s more elegant than the alternatives I was thinking about.

A post was split to a new topic: Can’t evaluate field isZero in type time.Time