Get First Date From History Param

Hi, can some on please explain why something like this not working? I would like to have custom variable “history” with manual entered dates when the page was updated. Here is an example.

my markdown: content/blog/about.md

---
date: "2022-01-01"
lastmod: "2022-11-11"
title: "About Page"
slug: "about"
categories:
  - "pages"
history:
  - "2022-03-10"
  - "2022-02-05"
  - "2022-01-01"
---
# My markdown page here
Something here

my html: /layouts/_default/single.html

{{ with .Params.history }}
  {{ . }}
{{ end }}

This works but outputs all dates, and I want just the first.
Result: [2022-01-01 2022-02-05 2022-03-10]

Now when I try doing something like this it will show an error instead of showing me first date on the list “2022-03-10”.

{{ index .Params.history 0 }}

I’ve also tried this and its not working =(

{{ with .Params.history }}
  {{ range first 1 (.Params.history) }}
    {{ . }}
  {{ end }}
{{ end }}

Thank you for any help!

It’s possible, but you need to get used to Go templating. Your syntax is wrong:

I’ll try to give you a correct version without testing this on my end:

{{ with (index .Params.history 0)  }}
{{ . }} 
{{ end }}

You missed the changing context in both of your examples. The dot inside the with already represents .Params.history. Therefore, you get an error when you try to access it again.

1 Like

Okay, thank you so much Georg. I got it working with something like this now.

To get first two dates:

{{ with .Params.history }}
  {{ range first 2 . }}
    {{ . }} <br>
  {{ end }}
{{ end }}
Result:
2022-03-10
2022-02-05

To get only one date.

{{ with .Params.history }}
  {{ index . 0 }}
{{ end }}
Result:
2022-03-10
1 Like

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