===============
== bacardi55 ==
===============
ἕν οἶδα ὅτι οὐδὲν οἶδα

Adding an alert on old posts with hugo

- Permalink
/!\ Warning: This article is older than 555 days, make sure the content is still relevant!

Very short post today, more as a side note for my future self. To add a message on old message, the minimal basic to add in the template is:

{{ $ageDays := div (sub now.Unix .Date.Unix) 86400 }}

{{ if gt $ageDays 365 }}
    Warning: This post is older than 365 days, the information may not be relevant anymore.
{{ end }}

Code is self explanatory, ageDay store the number of days between now and the post and is then compared to 365 in this example.

To add this to MinIndie, a minimal indieweb ready hugo theme, I added in _default/single.html the following:

<div class="e-content">
    {{- with .Site.Params.tagPostOlderThanXDays -}}
        {{- if gt $ageDays . -}}
            {{ partial "article-old-content.html" . }}
        {{- end -}}
    {{- end -}}
    {{ .Content }}
</div>

The article-old-content.html partial contains:

<div class="alert">
    <strong>/!\ Warning: This article is older than {{ . }} days, make sure the content is still relevant!</strong>
</div>

To enable this feature, the config.toml must contains a Params.tagPostOlderThanXDays, for example:

# Parameters
[params]
  []
  tagPostOlderThanXDays = 555
  []

With this, a message is now displayed on old post and it looks like this:

Figure 1: screenshot of a post showing an alert than the post is old

Figure 1: screenshot of a post showing an alert than the post is old

And voilà, thanks to this post for the inspiration :)


Contact

If you find any issue or have any question about this article, feel free to reach out to me via webmentions, email, mastodon, matrix or even IRC, see the About page for details.