Trying to make an agenda/event listing

Hi all,
I’m trying to list events for a theater website. Tried several approaches now but without any ‘luck’.

I’d like to make a list so that it will be possible to assign a date to a theater. The theaters must be accessible from within the data folder, so that you don’t have to type all the info again and again.

Of course the dates will be variable and in the future.

The theaters will be stored in a json file:

[
  {
    "Naam": "Theater name",
    "Plaats": "City",
    "Url": "www.theaterurl.nl/"
  }
]

Then, I have a section called ‘producties’ and a productie.md with:

agenda:
- datum: 2022-01-26T23:00:00+00:00
  theater: data/theaters/theater.json

And a single.html →

{{ with .Params.agenda }}

        {{ range $e := . }}
        <span>{{ $e.datum }}</span>

        <ul>
         <li>
          {{ range $.Site.Data.theaters.theater}}
           <a href="{{ .Url }}">{{ .Naam }}</a> {{ .Plaats }}  
          {{ end }}
         </li>
        </ul>

       {{  end }}

Now that works, But of course I don’t want the last value to be hard coded. I can’t just do obviously

{{ range $.Site.Data.theaters.$e.theater }}

However that’s the idea. I really have no clue how to rewrite it to make it readable for Hugo. I am trying now for a few days and am out of ideas (and out of search skills). Hope someone can toss me a line?

Thanks in advance!

https://gohugo.io/functions/index-function

1 Like

Thanks for pointing in the right direction!

I find this difficult to understand. We’re getting somewhere, but each time I’m think I’m near…

{{ index .Site.Data.theaters "groeneengel" }}

It’s defenitely there, output in single.html

[map[Naam:De Groene Engel Plaats:Oss Url:www.groene-engel.nl/]] 

Then the confusion starts: according to the manual

{{ index .Site.Data.theaters .Params.theater }}

… should work, but I have a nested item. So I get an error, as expected:

executing "main" at <index .Site.Data.theaters .Params.theater>: error calling index: value is nil; should be of type string

I thought that this

{{ index .Site.Data.theaters $.Param "agenda.theater" }}

might work, but I am getting an error about wrong number of args

executing "main" at <$.Param>: wrong number of args for Param: want 1 got 0

and I don’t know what I am doing wrong.

It would be helpful if shared your repository.

Made it public, hope it works
repo

Relevant parts:
data/theaters/
content/producties/rock-me-baby.md
layout/producties/single.html

1) content/producties/rock-me-baby.md

Change:

agenda:
- datum: 2022-01-26T23:00:00+00:00
  theater: data/theaters/groeneengel.json

To:

agenda:
- datum: 2022-01-26T23:00:00+00:00
  theater: groeneengel

2) data/theaters/groeneengel.json

Change:

[
  {
    "Naam": "De Groene Engel",
    "Plaats": "Oss",
    "Url": "www.groene-engel.nl/"
  }
]

To:

{
  "Naam": "De Groene Engel",
  "Plaats": "Oss",
  "Url": "www.groene-engel.nl/"
}

It’s a single object, not an array of objects.

3) layouts/producties/single.html

Something like:

{{ range .Params.agenda }}
  datum:  {{ .datum | time.Format ":date_long" }}<br>
  theater: {{ .theater }}<br>
  {{ with index site.Data.theaters .theater }}
    naam: {{ .Naam }}<br>
    plaats: {{ .Plaats }}<br>
    url: {{ .Url }}<br>
  {{ end }}
{{ end }}

4) Recommendation

Use lowercase or snake_case keys in your JSON files.

1 Like

Thanks a lot! for this piece of clean and elegant code…

{{ range .Params.agenda }}
    {{ .datum | time.Format ":date_long" }}
    {{ $trimmed := trim .theater "data/theaters .json" }}    
    {{ with index site.Data.theaters $trimmed }}
     - <a href="{{ .url }}">{{ .naam }}</a> - {{ .plaats }}
    {{ end }}     
  {{ end }}

My final code. I followed the recommendation except for this line:

theater: data/theaters/groeneengel.json

I wanted to keep it like that because that will be the front matter output that Forestry.io will give me. I have to work with a CMS because when the site is finished someone else has to update it. So I trimmed that in a variable and put the variable in the with statement.

Thanks again, you made my day!

1 Like

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