Join my Laravel for REST API's course on Udemy 👀

Check for future posts in Hugo templates

May 14, 2021  ‐ 2 min read

I have been happily running by blog using the Hugo static site generator. Hosted on Netlify, works like a charm. However, lately I has an issue where I, by accident, had set the publishing date for a post 10 years in the future. Not that big a deal, but in order to prevent this error in the future I did want to add a visual clue to future posts.

I never really researched the Hugo templating language very well. I just picked a theme from Hugo Themes, slightly modified some CSS and it just worked without any other effort.

Using an example from the Hugo website I was able to add a visual clue to future posts.

Lets for example say that my markdown post has the following YAML Front Matter at the top of the posts.

title: "Check for future posts in Hugo templates"
date: 2031-05-14
lastmod: 2031-05-14

I have been happily running by blog...

Now, in my templates I check check for that date property which is set in the future. I did this with {{- if ge .Date.Unix now.Unix }}. See the following snippet which adds some text after a title for future posts:

<h2>
  {{ .Title }} {{- if ge .Date.Unix now.Unix }}
  <sup>[future]</sup>
  {{- end }}
</h2>