Struggling with Template for my homepage

I am an it-student who’s into cybersecurity, I own a website built on Hugo and I’ve been trying to show my recent write-ups on my homepage. I’m no front-end developer by any means but I am exploring the space slowly but surely by trying new ideas with my website. Now because of that, I referred to ChatGPT but after hours of trying to prompt it ran in circles stating the date format isn’t the proper one and my markdown and code block don’t have the same format.

{{ define "main" }}

<h1>Welcome to my Cybersecurity blog!</h1>
{{ $time := time.Now.UTC }}
{{ $recent := where .Site.RegularPages ".Type" "write-up" }}
{{ $cutoff := $time.AddDate 0 -5 0 }}
{{ $cutoff = $cutoff.UTC.Format "2006-01-02T15:04:05Z" }}
{{ $filtered := where $recent ".Date.After" $cutoff }}

{{ printf "recent: %v" $recent }}
{{ printf "cutoff: %v" $cutoff }}
{{ printf "filtered: %v" $filtered }}

{{ if $filtered }}
  <h2>Recent Write-ups</h2>
  <ul>
    {{ range $filtered }}
      <li><a href="{{ .Permalink }}">{{ .Title }}</a> ({{ .Date.Format "Jan 2, 2006" }})</li>
    {{ end }}
  </ul>
{{ else }}
  {{ if $recent }}
    <p>No write-ups found after {{ $cutoff }}.</p>
  {{ else }}
    <p>No write-ups found.</p>
  {{ end }}
{{ end }}
{{ end }}

As far as I’m concerned the code above should run just fine. As it checks whether or not there are posts of the type “write-up” from 5 months ago which there is from Jan 20, 2023. If not, don’t show anything besides a paragraph stating nothing was found. However the debug print functions display this recent: Pages(1) cutoff: 2022-11-09T00:49:15Z filtered: Pages(0) No write-ups found after 2022-11-09T00:49:15Z. and this is where I’m stuck. It finds the page but won’t filter it and show the content. Where do I go with this? Any help is appreciated. I am using the hello-friend-ng theme btw.

Two problems.

1) When using the where statement, the arg must be a key, not a key.method.
2) Don’t convert the cutoff date to a string before making the comparison.

  {{ $time := time.Now.UTC }}
  {{ $recent := where .Site.RegularPages ".Type" "write-up" }}
  {{ $cutoff := $time.AddDate 0 -5 0 }}
  {{ $filtered := where $recent ".Date" "ge" $cutoff }}

I see. It works now thanks a lot. You were a huge help.

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