Load individual JSON files for each page

Hey! I’ve been using Go for a few years now on the backend and now I’m giving it a Go (geddit?) on the frontend.

There’s something I am trying to do, maybe Hugo it doesn’t work like that or I’ve got it completely wrong, you tell me.

So, I’ve got a section called “discography”, inside I’ve got two albums “album1.html” and “album2.html”. I want each album to be in it’s own page, I don’t care about a list of albums.

In “layouts” I’ve got a “discography” directory and inside a “single.html” file.

Inside the “single.html” file, I’ve got a partial called album that displays the album information.

// layouts/discography/single.html
{{ define "main" }}
{{ partial "album" . }}
{{ end }}

Then, to test it, I load a json file, like so:

// layouts/partials/album.html
{{ $data := getJSON "static/album1.json" }}

it works :white_check_mark:

So far so good, it works just fine. However this way I still have to create two identical layouts for each album, and I started to wonder, instead of having two identical partials (album), wouldn’t it be neat if I had ONE layout and fill it with data from a json file?

:one: So far, I can’t. I’ve tried this:

In the layotus/partials/album I’ve tried to load the json file depending on the album requested.
So, I thought, well, let’s send the file url from the content/albums/album1.html using FrontMatter, such as:

// content/discography/album1.html
json_file: "static/album1.json"

and then, in the layouts/partials/album.html load it up:
{{ $data := .Params.json_file }} well, nope :x:

Hugo complains:

Failed to get JSON resource "": read /Users/transistor/Documents/WebServer/dem-hugo3: is a directory

What gives?!

Ok, so maybe not a fan of reading the .Param directly, I tried:

{{ $json:= printf "%s" .Params.json_file }}
{{$data := getJSON $json}}

well, nope :x:
but Hugo complains with a different error:

Failed to render pages: render of "page" failed: execute of template failed: template: discography/single.html:3:3: executing "main" at <partial "album" .>: error calling partial: "/Users/transistor/Documents/WebServer/dem-hugo3/layouts/partials/album.html:7:11": execute of template failed: template: partials/album.html:7:11: executing "partials/album.html" at <getJSON $json>: error calling getJSON: Failed to create request for getJSON resource %!s(<nil>): parse "%!s(<nil>)": invalid URL escape "%!s"

and, you probably guessed it by now, the offending line 7 is:

{{$data := getJSON $json}}

Ok, so maybe the printf was too much, how about:
{{ $json:= .Params.json_file }}

again, nope :x:
same error as the first attempt:

Failed to get JSON resource ""

:two: I’ve also tried to use

{{ $data := $.Site.Date.albums.album1 }}

which, again, works fine when the $.Site... is fixed, but if I try to use a variable, doesn’t work.

So, or Hugo doesn’t support this way of creating content or I’m just very confused (honestly, it’s probably just me trying to do something off from what Hugo does, idk).

I there a way to do this? tl;dr bottom line:
I want to have one layout for all the albums, which is filled with data from a json file. And I want to create static pages, not loading dynamically on the server.
Is there another way?

Thank you for reading, looking forward to your input.

It would be best if you shared a repository with a sample project that reproduces the issue you encountered for us to see the full context of the templates.

Also see the Requesting Help guidelines

Were you able to fix this ? It’s driving me crazy

"{{ .Params.feed_url }}"

returns :


But :

{{ $d := getJSON .Params.feed_url }}

Returns :

Failed to get JSON resource "": read /Users/yann/Archives/Developpement/GeekTool/web: is a directory  logged 1 error(s) 

Finally found out what was going on.

For some reason, the fact that I did’t have feed_url defined on siblings pages was throwing this error.
It seems it’s trying to fetch all *.Params.feed_url