Implementing a Last Modified feature for posts

Hello,
I want to implement a feature that allows me to specify a last modified date in the front matter for each post, which will then be displayed in the post. I tried adding #+lastmod in the front matter, which works, but when it’s missing, it falls back to the modified value and the conditional still executes. To fix this, I added this into hugo.toml:

[frontmatter]
   lastmod = ['lastmod']  # no fallback values

Then in the template I can use:

{{ with .Lastmod }}
<div class="post-lastmod">
  <p>Last modified on:
    <time datetime="{{ . | dateFormat "2006-01-02" }}">{{ . | dateFormat "January 2, 2006" }}</time>
  </p>
</div>
{{ end }}

Considering that modifying the hugo.toml file in this case is more of a custom option, a deviation from the default behavior, I’m thinking that using the lastmod option might not be the best choice. I’m curious: for those of you who have implemented such a feature, what name do you typically use in the front matter for this purpose?

If you want that behavior where’s the problem to use the configuration option hugo offers?

If one doesn’t like that for his blog he could reset it to the default (or anything he likes). So might be valuable for the documentation.

guess having a non standard name might be counter intuitive.

By using the default value lastmod: I also need to add:

[frontmatter]
   lastmod = ['lastmod'] 

Otherwise, the while loop executes even if the lastmod is not present in the front matter. If it’s present it results in a valid date (the date I specified) but if not, it ends up with an empty date value.

With a custom name you would have to tell that they cannot use the lastmod but have to use yours.

The with will still be necessary. So one config setting instead of a custom name…

What if a user does not like yours…

He has to change your template…

Thank you. The template will only be used by me, so I’ll go with the lastmod option and set no fallback in hugo.toml

1 Like