Using dates from front matter


#1

Hi

Complete newbie to all things Hugo. Have an issue with “Time”. So I have Front Matter on posts (section “rides”) which includes a ride_date as well as a created date.

This code works

{{ range (.Data.Pages.ByParam "ride_date") }}
        <h2><a href='{{ .Permalink }}'> {{ .Title }}</a> </h2>
        <div>Ride date - {{ .Params.ride_date }}</div>
        <div>Date added - {{ .Date }}</div>
        <div>Start Time - {{ .Date.Hour }}:{{ .Date.Minute }}</div>
{{ end }}

This code doesn’t

> {{ range (.Data.Pages.ByParam "ride_date") }}
>         <h2><a href='{{ .Permalink }}'> {{ .Title }}</a> </h2>
>         <div>Ride date - {{ .Params.ride_date }}</div>
>         <div>Date added - {{ .Date }}</div>
>         <div>Start Time - {{ .Params.ride_date.Hour }}:{{ .Params.ride_date.Minute }}</div>
> {{ end }}

Error while rendering “section” in “rides/”: template: rides/list.html:8:36: executing “rides/list.html” at <.Params.ride_date.ho…>: can’t evaluate field hour in type interface {}

I’m stumped because the ride_date is correctly formatted in front matter

title = "Breakfast in Bampton"
date = 2018-02-23T17:10:03Z
draft = false
categories = [“Group Ride”]
ride_date = 2018-03-02T17:15:00Z

and outputs as UTC as does the .Date

If this is obvious, then I’m obviously missing something pretty fundamental here and would welcome some help.

Thanks M


#2

Hour is part of the built-in Hugo dates. It’s not available for your custom param ride_date.

You should probably specify it in a separate param.


#3

I don’t think that’s the problem… the problem is that the ride_date param is missing in one of his content pages.

With ride_date = 2018-03-02T17:15:00Z (without double quotes), hugo auto-casts that to time.Time type.

So the .Hour, .Minute, … work.


Update

Here’s a quick test case:

You’ll find the link to Markdown source in there… and here’s the relevant portion from the template:

{{ with .Params.foo }}
    Hour: {{ partial "debugprint.html" .Hour }}

    Minute: {{ partial "debugprint.html" .Minute }}
{{ end }}

Ref


#4

Thanks both

I checked the front matter but maybe I got ‘something’ wrong.

M


#5

Try something like this:

{{ range (.Data.Pages.ByParam "ride_date") }}
    {{ $page := . }}
    {{ with .Params.ride_date }}
        <h2><a href='{{ $page.Permalink }}'> {{ $page.Title }}</a> </h2>
        <div>Ride date - {{ . }}</div>
        <div>Date added - {{ $page.Date }}</div>
        <div>Start Time - {{ .Hour }}:{{ .Minute }}</div>
    {{ end }}
{{ end }}

From here:

If the parameter is not found at all in some entries, those entries will appear together at the end of the ordering.

So using ByParam does not guarantee that it will range only through posts with the specified param. The snippet I pasted above ensures that the .Hour, etc. date-functions are called only for posts that have the ride_date in front-matter.

I tested it locally and it works… As I have just one test post with that custom date in front-matter, I got just:

image


#6

Thanks kaushalmodi

Perfect - I have two pages that are NOT rides. But I was using this in the context of rides so thought I’d excluded them.

Sorted the issue perfectly

M


#7

Glad that it worked.

Actually a syntax like below should work, but I am not sure how to make that work with date keys… hopefully someone more knowledgeable with the use of where and date parameters can chime in.

Does not work

{{ range where .Data.Pages "Params.ride_date" "ne" nil }}
    <h2><a href='{{ .Permalink }}'> {{ .Title }}</a> </h2>
    <div>Ride date - {{ .ride_date }}</div>
    <div>Date added - {{ .Date }}</div>
    <div>Start Time - {{ .ride_date.Hour }}:{{ .ride_date.Minute }}</div>
{{ end }}

#8

A hook onto this thread. How does one format custom dates input in the front matter?


#9

I haven’t tried but dateFormat should work.