published on 22.08.2020
edited on 23.01.2021

I like to display “published date” and “last modified date” info for my posts. Date info comes from the date parameter and last modified info comes from the lastmod parameter, both defined in the front-matter.

1
2
date: "2020-08-22"
lastmod: "2020-08-22"

Get Date-Info Partial

To get date-info I’ve created a simple partial template. Which basically, checks if the “lastmod” and publish dates are the same then renders the dates. To compare two dates, we should format them using the same date format. “02.01.2006” is equal to “dd.mm.yyyy” in the Go language.

1
2
3
4
5
6
7
{{ $date := .Date.Format "02.01.2006" }}
{{ $lastmod := .Lastmod.Format "02.01.2006" }}

<span class="date-info italic">published on {{ $date }}</span>
{{ if ne $lastmod $date }}
    <span class="date-info italic"> <br> edited on {{ .Lastmod.Format "02.01.2006" }}</span>
{{ end }}

Get LastMod Using Git

There is a very cool feature in Hugo, that can get the “lastmod” info from git. You need to enable it in the config file: config.toml.

1
enableGitInfo = true

Still, if lastmod parameter exists in front-matter, Hugo will use the date from the from-matter, not from git. So, you can freely enable the feature and use the parameter whenever you want to manually enter another date.

I realized that Hugo still using the lastmod info that is coming from git even if we set the lastmod parameter in the front-matter. The good thing is we can change this behavior by configuring front matter.

1
2
3
4
5
[frontmatter]
date = ["date", "publishDate", "lastmod"]
lastmod = ["lastmod", ":git", "date", "publishDate"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]

By default, ":git" comes before "lastmod". To override it, we need to add these lines to the config file.

You are reading the 9th of 19 episodes in Hugo.
Published on 22.08.2020 by Mert Bakır. Last update on 23.01.2021 with commit 3a172e2.
hugo
#hugo #web-dev
Next episode:
Convert Dates Into Your Language in Hugo
published on 22.08.2020

We talked about rendering dates and the last modification date in the last post. Now, we’ll look into how can we convert month names into another language? I’ve had a similar experience last year with jekyll. The idea is the same, we need to map each month to their version of the …

published on 29.08.2020

In Hugo and generally other static website generators, we are using data files to store data. These files can be in YAML, TOML, or JSON formats. You can always read more about data template in the original documentation. Yet, I want to give some examples from my blog. First of all, converting dates …

published on 30.08.2020

The question is clear. We need to pass variables or arguments in Hugo’s partials. We usually use only the dot when calling partials. {{ partial "foo.html" . }}. The dot means the current page’s context. All it’s variables like .Title, .Permalink, .Content and all others …

published on 24.01.2021
edited on 11.06.2021

Some time ago, I wanted to display image galleries on my Hugo website and searched for Hugo themes for photography and gallery. I can’t say I find much. Then, I met with a javascript library called nanogallery2 which is using another javascript library as an image viewer lightbox2. In this …

published on 23.01.2021
edited on 11.06.2021

Image processing may seem complicated at first but it’s actually easy and definitely worth implementing since it’ll help you decrease page load times. As you probably know, we don’t want to load raw images with huge sizes for small thumbnails or blog-posts. We want to load a small …

published on 29.11.2020
edited on 05.12.2020

Plotly is a visualization library that allows us to write code in Python, R, or Julia and generates interactive graphs using Javascript. So, we don’t have to deal with Javascript. You can checkout Plotly gallery, there are interesting works. Anyway, last week, I’ve started learning …

published on 18.09.2020
edited on 13.02.2021

First of all, if you don’t know about data files, you may want to start reading from data files in hugo. This post is about a solution for a very particular problem. How can we use “group by” for the data from data files? Let me clarify with an example. I was creating a single page …