Newb question: Looping through date-based front matter

Given a set of templates that fit this pattern:

/content/events/2015-10.md

+++
date = "2015-10-30"

[[event]]
date = "2015-10-05"
title = "event title"
category = "taxonmy_name3"

[[event]]
date = "2015-10-02"
title = "event2 title"
category = "taxonmy_name3"
summary = """
Some Copy about the event"""

[[event]]
date = "2015-10-06"
title = "event3 title"
category = "taxonmy_name3"

+++
Some copy about the month's events

I’ve set this up because it’s easy for content creators to make a list of events for each month. I’m attempting to pull in the entries, date-based, but also the events within each month, date-based, as well as taxonomies.

Is this possible? So far, I’ve managed to display the months with their events, but not to display only upcoming (front matter-based) events.

// Loop through all events content
{{ range where .Data.Pages.ByDate "Section" "events" }}
  // Only show events entries that are in the future
  {{ if ge .Date.Unix .Now.Unix }}
    // Only show events entries that have an event in their front matter
    {{ if isset .Params "event" }}

      <h2>{{ .Date.Format "January" }} {{ .Type | title }}</h2>
      <ul id="tags">
        // loop through front-matter-based events
        {{ range .Params.event }}
        // THIS IS WHERE I'M STUCK 
        {{ if ge .event.date.Unix .Now.Unix }}
          <li style="margin-bottom:3em;">
            {{ .date | dateFormat "Monday, Jan 2, 2006"  }}
            {{ if .title }}            
                {{ .title | markdownify }}            
            {{ end }}        
            {{ if .summary }}
              {{ .summary | markdownify }}
            {{ end }}
          </li>
        {{ end }}
      {{ end }}
    </ul>

    {{end }}
  {{ end }}
{{ end }}

just diving in to taxonomies now. Doesn’t look like they work at that level. Any suggestions appreciated.

You note that you’re stuck but doesn’t say why/what. Would be easier with a failing Git repo.

I’m stuck here: {{ if ge .event.date.Unix .Now.Unix }} - it does nothing.

This was just a test install, but I can throw it up on a repo. brb

Here’s the repo


page where that code is:

here’s the public folder on the web:
http://ceaseless-beds.surge.sh/

You’ll see that the first two events are Oct. 2 and 5, and what I expect to do is for them to not show because of their dates.

So what I thought someone would say when I posted this was “No, you can’t do that with dates in front matter.” or “sure, you can do that, you’re just writing it wrong.” So barring either of those two responses, I suppose there’s some potential for this? I’ll just keep at it then.

The problem is, this will be treated as a string. You will have to look into how dates are supposed to “look like” in the TOML spec:

Perfect, thanks! Once I made the front matter events proper dates it worked.

Though I used this:

{{ range where .Params.event “date.Unix” “>” .Now.Unix }}

Instead of the conditional statement I had in previously.

You should be able to drop the .Unix.

Thank you. It seems to work with the .Unix and not otherwise.

Ah, OK – but in the latest 0.15-DEV Hugo you should be able to work with dates directly in where, but your construction works fine.

1 Like