Equivalent code to ifchanged Django template tag

Is there a Go or Hugo equivalent to Django’s ifchanged template tag?

I am converting a blog from Django to Hugo, and need a date line to appear amid a list of posts only if the current day, year and month is different from the last batch of posts. This is the Django template code:

{% ifchanged %}
    <h2>{{ object.pub_date|date:"l, d F Y" }}</h2>
{% endifchanged %}

I can’t test it at the moment, but am wondering if something like this would work:

{{ if not eq .Date.Format "Monday, 2 January, 2006" }}
    <h2>{{ .Date.Format "Monday, 2 January, 2006" }}</h2>
{{ end }}

When I manage to test, I’ll come back and post the answer, or more questions.

i guess you have to do something like (from memory & not tested)

{{ $postDate := .Params.myPostDateFromFrontMatter | time }}
{{ if ne now $postDate }}   
...
{{ end }}
1 Like

Thank you!

I will report back as soon as I have tested this.

Thanks, @divinerites — your solution worked, but what I really needed was a little bit different from my question, and I only realized it after further thought; What I wanted was the equivalent of Django’s {% if not forloop.first %} template tag — a way to skip the first item in a range, specifically so that a date line would only appear after the first post in a list.

This is the solution that worked for me, with help from this post:

{{ $.Scratch.Set "counter" 0 }}
{{ range .Site.RegularPages.GroupByDate "Monday, 2 January 2006" }}
  {{ $.Scratch.Set "counter" (add ($.Scratch.Get "counter") 1) }}    
  {{ if ne ($.Scratch.Get "counter") 1 }}
    <h2 class="dateline">{{ .Key }}</h2>
  {{ end }}
    …display articles, etc…
{{ end }}

Thanks for your help!

1 Like

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