Function / conditionals

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?

I’d wrap the whole snippet like this:

{{ 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.

Oh ))
Smart. Didn’t think this way but makes sense and solves the problem.
Thanks a lot

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.

Yeah, that too.

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:

{{- $delta := .Date.Sub .Lastmod -}}{{- if lt $delta.Hours -3 -}}

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 :wink: .

out of curiosity could you please share the whole code you use for the same case?

Absolutely. The piece of partial (invoked at every non-list page) goes like this:

{{- $delta := .Date.Sub .Lastmod -}}
{{- if lt $delta.Hours -3 -}}
<div class="post-info-mod">
<span>
{{- $date := partial "plugin/date" (dict "date" .Lastmod "ctx" .) -}}
<time class="dt-updated" datetime="{{ .Lastmod.Format "2006-01-02T15:04:05-0700" }}">
<a href="{{ $.Permalink }}" title="{{ .Lastmod.Format "02.01.2006 15:04 MST" }}">
{{- dict "Date" $date | T "updatedOnDate" -}}
</a>
</time>
</span>
</div>
{{- end -}}

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:

{{- if eq .ctx.Site.Language.Lang "ru" -}}{{ .date.Day }} {{ index .ctx.Site.Data.mes ( printf "%d" .date.Month ) }} {{ .date.Year }}{{- else -}}{{ .date.Format (.ctx.Site.Params.dateformat | default "Jan 02, 2006") }}{{- end -}}

of course, it needs the data/mes.yaml with months names in Russian (in Genitive, actually) to be present:

1: "января"
2: "февраля"
3: "марта"
4: "апреля"
5: "мая"
6: "июня"
7: "июля"
8: "августа"
9: "сентября"
10: "октября"
11: "ноября"
12: "декабря"

And the corresponding i18n is (for English):

[updatedOnDate]
other = "Updated on {{ .Date }}"

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.

thanks !
much more “complex” than with what I ended up. I will have a close look to try to understand the logic but again thanks for sharing.

<!-- Article Date-->
<div>
    {{ $ageDays := div (sub now.Unix .Date.Unix) 86400 }}
    {{ if eq $ageDays 0 }}
    <p>Published: Today</p>
    {{ else }}
    <p>Published: {{ .Date.Format "January 2, 2006" }} </p>
    {{ end }}
</div>

<!-- Last Modified Article Date-->
<div>
    {{ $ageDays := div (sub now.Unix .Lastmod.Unix) 86400 }}
    {{ if eq .Page.Date .Page.Lastmod }}
    <p>Updated: Never</p>
    {{ else if eq $ageDays 0 }}
    <p>Updated Today</p>
    {{ else }}
    <p>Updated {{ .Lastmod.Format "January 2, 2006" }} </p>
    {{ end }}
</div>

This “complexity” is all i18n and polishing, yours actually has more complex logic, since I never bothered with the “Today” thing.

One thing I would do differently is avoid the extra conversion to time.Unix, i.e.:

{{ $age := now.Sub .Lastmod }}{{ $ageDays := div $age.Hours 24 }}

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 :wink:

1 Like

Also you could use the cond function to achieve the same, in a slightly terser way.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.