How to modify the date parameter of a page?

Hi Hugo community

I’m working on a feature to support recurring events on a Hugo page. To do so, my approach is as follows: in the Front Matter, I add recurrence: (which can be set to weekly or monthly) and recurrence_end to define until when the recurrence has to be taken into account.

Then on the page I go through all events and add “new” events. My approach to add these “new” events is to copy the current event and then modify the dates accordingly.

{{ $events := .Pages }}
   {{ range sort $events "" "asc" }}
      {{ if (and .Params.recurrence .Params.recurrence_end) }}
         {{ if eq .Params.recurrence "monthly" }}
            {{ $event := . }}
            {{ $date := $.Param "date" }}
            {{ $date := $date.AddDate 0 1 0 }} 
            {{ $ := $date }}
            {{ $events = $events | append $event }}
         {{ else if eq .Params.recurrence "weekly" }}
            {{/* {{ $events = $events | append . }} */}}
            {{ $event := . }}
            {{ $date := $.Param "date" }}
            {{ $date := $date.AddDate 0 0 7 }} 
            {{ $ := $date }}
            {{ $events = $events | append $event }}
      {{ end }}
   {{ end }}
{{ end }}

But Hugo complains about {{ $ := $date }} with unexpected ":=" in operand. My question is now, how can I modify the date (and other params in the Front Matter) of a copy of an event?

A Page is immutable. Yes, you made a copy of the Page but it is still a Page.

In this case, how can I get a mutable version of the Page?

Extract the values you need into a map or something.

Thanks for your hint @jmooring. Could you provide me an example? I’m not very used to Hugo’s way of variable handling.

This requires Hugo v0.95.0 or later.

git clone --single-branch -b hugo-forum-topic-37389 hugo-forum-topic-37389
cd hugo-forum-topic-37389
hugo server

Be advised that the .AddDate method produces results that are “expected” by the Go team, but certainly not expected by me.

func main() {
	start := time.Date(2023, 01, 31, 0, 0, 0, 0, time.UTC)
	fmt.Printf("%v\n", start.AddDate(0, 1, 0))

2023-03-03 00:00:00 +0000 UTC

Closing because this is working as documented and expected.

Thanks a lot @jmooring for the example. I did learn a lot from it. There is a last problem I’m facing.

Within {{ range sort $events "Date" }}{{ end }} (lines 47-52), I seem to have no access to .Page. E.g. {{with .Page.GetPage "/act/events"}}{{.Params.learn_more}}{{end}} doesn’t give me anything back.

That is correct, because you are ranging over a slice of maps, not over a page collection.

You need to add .Params.learn_more to each map as you range through the page collection.

I’ve update the repository. Pull changes and try it.

If you’re going to add more custom fields, it would make sense to include the .Page object when building the slice of maps.

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