Hi
I guess it’s pretty simple / easy but … I can’t find (based on my little to none experience with Hugo).
I have the following code (works fine) :
{{ $ageDays := div (sub now.Unix .Date.Unix) 86400 }}
{{ if eq $ageDays 0 }}
<p>Updated today.</p>
{{ else if lt $ageDays 1 }}
<p>Updated {{ .Lastmod.Format "January 2, 2006" }} </p>
{{ end }}
But I would like to display nothing when there never been an update to the blog article.
I have created in frontmater : updated : true / false.
I would like to add this logic that is the article has never been updated we don’t display anything.
How you would manage this based on my existing code?
{{ if .Date.Equal .Lastmod }}
Page has never been modified, yay! (just delete this line to have no output here)
{{ else }}
here goes your code
{{ end }}
What happens here in the first line is the check whether current page’s .Date is the same as .Lastmod. Depending on your setup, you may want to use .PublishDate instead of .Date here.
I think with @nekr0z’s code you won’t need to set a frontmatter variable updated. Just in case that was not clear. If the publishing date and the last modification date differ, then the page has been modified.
Also, when I did a similar thing for my own website, I found that strict equality checks don’t really work if you rely on GitInfo for .LastMod, as the exact time of commit is never the same as the .Date you put in the front matter.
What I eventually ended up doing on my site is give it a grace period of 3 hours. If .LastMod is less than 3 hours after the .Date, I consider the page never updated (I tend to re-read a post immediately after I publish it, and spot an occasional typo more often than not; I don’t consider fixing these typos “updates”, and I give myself 3 hours for that), my website — my rules. So the actual check I use is:
Observe how I’m looking for a negative value here, since in this case $delta is negative if .Lastmod is later than .Date (which is exactly what we’re looking for).
I think there is a way with {{ datevariable | time }} and then you can compare those objects, which are created based on a string that is used in datevariable. Sometimes the archetype defines those three date fields differently. But I did not dive deep enough on that one. No need yet .
As you can see, the date here is formatted in three different ways: ISO 8601 for the datetime attribute, a bit more human-readable for the title, and for the text itself it is formatted in a separate partial and then internationalized (my site is bilingual). The separate plugin/date partial takes care of translating the months names into Russian and consists of a single line:
You can see the result of this whole code working on this page, for example. It’s in the lower part of the page, below the footnotes, just above the tags. This is how it works if the post hasn’t been updated.
but I guess that’s just a matter of taste more than anything else, I don’t expect the render time difference be of any significance for a site with less than 10k pages