Events Loop that includes today

I am trying to range through upcoming events, and I found this on the HUGO site: https://gohugo.io/templates/introduction/#example-show-only-upcoming-events

        <ul class="upcoming-events">
        {{ range where .Data.Pages.ByDate "Section" "events" }}
          {{ if ge .Date.Unix now.Unix }}
            <li>
              <span>{{ .Date.Format "2 January at 3:04pm" }}</span>
              — {{ .Title }}
            </li>
          {{ end }}
        {{ end }}
        </ul>

However, this excludes events that are happening today. How can this be modified to include the events that are happening today?

You want events that are happening today listed on your site.
That way, people who are coming to the event today have an easy way of finding the event details, location, etc…

Here is the fix:

Change the now.Unix to:

(now.AddDate 0 0 -1)

See .AddDate for reference: https://gohugo.io/functions/adddate/

Many thanks! I was thinking on how to solve this problem today!

I have this on my homepage, pulling only the first 4 upcoming events including the ones with today’s dade:

  {{ $events := where .Site.RegularPages.ByDate "Section" "events" }}
  {{ range where $events "Date" "ge" (now.AddDate 0 0 -1) | first 4 }}
      {{ partial "event-card.html" . }}
  {{end}}
1 Like

And this is another way to do that. This how I solved on the event page list when I wanted to show the event during all of its period (when it has both start and end dates). Can be improved, for sure, but it works.

{{ range .Data.Pages.Reverse }}
{{ $now := now.Format "2006-01-02" }}    
{{ $start := .Date.Format "2006-01-02"  }}

{{ if .Params.End_date }}
    {{ $end := dateFormat "2006-01-02" .Params.End_date  }}

    {{ if or (ge $start $now) (ge $end $now)}}
        {{ partial "event-list.html" . }}
    {{ end }}

{{ else if ge $start $now }}
    {{ partial "event-list.html" . }}                        
{{ end }}
{{ end }}
1 Like

Now I am trying to figure out — how can I get ONLY the past events, no future events?

{{ if ge .Date.Unix (now.AddDate -1 0 0 ) }}
   {{ .Render "past"}}
{{ end }}

This doesn’t seem to be working. I guess that is getting all the events posted since a year ago.

See https://golang.org/pkg/time/

{{ if .Date.Before now }}

2 Likes

@sjardim — is this still working for you? I just upgraded to 0.44 and stopped working. Wondering what you’re using now?

I am upgrading for 0.26 to 0.44 and confused on how to now render a list of upcoming events.

According to this comment by @bep, https://github.com/gohugoio/hugo/issues/3977#issuecomment-379154169 there appears to be associations that can be added to the config.yml, but I am not clear on what those associations are. Also not clear if this will require us to change the front-matter across all of our events.

Is there a place in the docs that talks about how to best render events?

Fixed !
In upgrading HUGO, I didn’t realize that we needed to add: buildFuture : true to our config.yml file.

Thank you @budparr :tada:

2 Likes