Hi all,
I’m upgrading our version of Hugo, so am updating references from getJSON to resources.Get (for a local file) and getting an “Unable to get global resource” error.
Before:
{{ $versionfolder := replace (lower (.Page.Params.version)) " " "/" }}
{{ $file := printf "%s" "/glossaryitems.json" | printf "%s%s" $versionfolder | printf "%s%s" "content/en/platform/" }}
{{ $glossarylist := getJSON $file }}
After:
{{ $file := printf "%s" "/glossaryitems.json" | printf "%s%s" $versionfolder | printf "%s%s" "content/en/platform/" }}
{{ $glossarylist := dict }}
{{ with resources.Get $file }}
{{ with . | transform.Unmarshal }}
{{ $glossarylist = . }}
{{ end }}
{{ else }}
{{ errorf "Unable to get global resource %q" $file }}
{{ end }}
I’m guessing the error is because the JSON find is not in the assets folder:
“This function operates on global resources. A global resource is a file within the assets directory, or within any directory mounted to the assets directory.”
Is there any way to write the above without having to move the location of the JSON file?
P.
Where’s the JSON file in location to the project root?
An example would be:
/content/en/platform/corda/5.2/glossaryitems.json
Is that a page bundle? For example:
content/
├── en/
│ └── platform/
│ └── corda/
│ └── 5.2/
│ ├── glossaryitems.json
│ └── index.md
└── fr/
└── platform/
└── corda/
└── 5.2/
├── glossaryitems.json
└── index.md
It’s actually a branch bundle, as the file at the same level as the glossaryitems.json is a _index.md.
P.
So, like this?
content/
├── en/
│ └── platform/
│ └── corda/
│ └── 5.2/
│ ├── _index.md
│ └── glossaryitems.json
└── fr/
└── platform/
└── corda/
└── 5.2/
├── _index.md
└── glossaryitems.json
If this structure is correct, are you rendering the glossary in the section template for content/LANG/platform/corda/5.2/_index.md
?
There’s a glossary shortcode (themes\doks\layouts\shortcodes\glossary.html) which is where the code in my first post is located.
This shortcode is used in multiple versions, and locations. An example would be the file \content\en\platform\corda\5.2\reference\glossary.md:
# Glossary
{{< glossary >}}
The glossary shortcode uses the version para param to get the correct version:
{{ $versionfolder := replace (lower (.Page.Params.version)) " " "/" }}
{{ $file := printf "%s" "/glossaryitems.json" | printf "%s%s" $versionfolder | printf "%s%s" "/en/platform/" }}
{{ $glossarylist := dict }}
{{ with resources.Get $file }}
{{ with . | transform.Unmarshal }}
{{ $glossarylist = . }}
{{ end }}
{{ else }}
{{ errorf "Unable to get global resource %q" $file }}
{{ end }}
{{ range $term, $value := $glossarylist }}
{{ $termkebab := lower (replace $term " " "-") }}
{{ with $value.more }}
<details><summary><h4 id="{{ $termkebab }}">{{ $term }}</h4>{{ $value.definition }}</summary>{{ safeHTML $value.more }}</details>
{{ else }}
<p style="margin-top: 1rem";><h4 id="{{ $termkebab }}">{{ $term }}</h4>{{ $value.definition }}</p>
{{ end }}
{{ end }}
P.
Please answer my question. Is the structure above correct?
The (partial) structure is:
content/
├── en/
│ └── platform/
│ └── corda/
│ └── 5.2/
│ ├── _index.md
│ ├── glossaryitems.json
│ └── reference
└── glossary.md
The glossary is rendered in glossary.md by using the glossary shortcode. There is also a tooltip shortcode that uses the glossary.json file to render tooltips through other pages under platform/corda, using the glossary.json file for the appropriate version.
P.
If you convert glossary.md into a page bundle you can place the json file adjacent to it, then use .Page.Resources.Get
in your shortcode. Otherwise, in your shortcode, you’ll have to capture the page first (.Page.GetPage "/platform/corda/5.2"
) then use .Page.Resources.Get
.