Accessing another pages resource

Hi,

I am updating my website to use hugo version 0.131.0 rather than the existing 0.92.0 version.
Hopefully the only issue I have is to phase out my use of getJSON.
So far so good and I have resolved three out of my four getJSON calls using the page resource guidance in this page.

The remaining issue I have is that I was successfully accessing JSON files in one page from another page.

My JSON files live in pages under my results sections:

results/2023-24/civilservice-summer-cup/civilservice_edsummercup_2023_24.json
results/2022-23/csbos1/csbos1_sncl_2023.json
results/2022-23/csbos2/csbos2_sncl_2023.json

When rendering pages in my news section, I used a shortcode that used getJSON to access a subset of the data from the JSON file in my results section.

{{< league_scorecards json="results/2023-24/civilservice-summer-cup/civilservice_edsummercup_2023_24.json" opp="Edinburgh West">}}

Essentially I formed a filepath to the JSON file’s location and getJSON worked just fine.

{{ $json_data_filepath := path.Join "content" (.Get "json") }}
{{ $json_data := getJSON $json_data_filepath }}

This no longer works as the revised code calls .Resources.Get (or .Page.Resources.Get) and is expecting the named JSON file to be a local resource for the news page. And is not willing to load a JSON resource from a page in the results section.

Is my only option to relocate all my JSON files from their current location in my results section to the global “assets/data” area?

I can do this but it is a pity as the results section is a convenient place to store them.

Assuming I do have to move them to the “assets/data” area, is it allowed to have a nested directory structure to help organise the files sensibly?

assets/data/results/2023-24/civilservice-summer-cup/civilservice_edsummercup_2023_24.json
assets/data/results/2022-23/csbos1/csbos1_sncl_2023.json
assets/data/results/2022-23/csbos2/csbos2_sncl_2023.json

Thanks,
Crawford.

You can access a pages resources by first loading the page and then… accessing it’s resources…

I think something along the lines of this might work:


  {{ $page := site.GetPage "nameofthepagewiththeresources" }}
  {{ $json := $page.Resources.GetMatch "*.json" }}

In general, think about how the json “fits” into the structure of your site. Is it in-separately connected to that single page where it is now or is it something you access independent from any page? If the latter then restructure (site.Data.my.folder.jsonfile is easier to access). If not, then write a partial that “gets” your JSON data based on the page bundle it is in.

1 Like

Thanks. Good to be aware of option to load a page. Does not feel appropriate in my use case so I suspect I will move all my JSON files to assets and update accordingly. There are quite a few to move! Oh well time for a few quick scripts…

You may want to have a look at the mount options. You could selectively mount page resources (with pattern /results**/*.json) to data or assets.

That way you can keep the data close to the pages and refer to them from other pages using GlobalResources (assets) or as .Data. (data)

Module configuration: mounts

1 Like