Hello,
I am trying to load json data using a dynamic path, using variables that are specified in Front Matter of my dot md files.
My data structure is as follows
data/guide/<name>/<date>.json
I have tried using printf to build up the path and the various solutions offered by these posts like below
Hello Hugo users,
I am building my own blog with my own template using hugo. All of this has been a very steep learning curve for me, but I was able to resolve all challenges so far and Iβm happy with the results and liking Hugo very much. I am almost done, but got stuck on the following.
I am trying to build a commenting system where I will have a YAML file for each comment, storing the comment files for each blog in their own directory. The relevant part of my project structure looks like thβ¦
I also tried this solution with setting a dataFile in the front matter
{{ $dataFile := index .Site.Data.guide .Params.datafile }}
But get the following error
guide is not a method but has arguments Rebuild failed:
executing βmainβ at <index .Site.Data.guide .Params.datafile>: error calling index: value is nil; should be of type string
I have also tried to simplify the directory path to
data/guide/<name>-<date>.json
but this does not help either.
All suggestions welcomed.
Thanks in advance
gearoid
Structure
data
βββ guide/
βββ a/
β βββ 2021-06-07.json
βββ b/
βββ 2021-06-01.json
Example of TOML front matter:
+++
title = "Test"
date = 2021-06-05T07:12:16-07:00
draft = false
[guide]
name = "a"
date = 2021-06-07
+++
Example of YAML front matter:
title: Test
date: 2021-06-05T07:12:16-07:00
draft: false
guide:
name: b
date: 2021-06-01
layouts/_default/single.html
{{ with .Params.guide }}
{{ $data := index site.Data.guide .name (dateFormat "2006-01-02" .date) }}
{{ end }}
1 Like
You are a gentleman @jmooring thanks very much for the prompt reply and the clear solution.
1 Like
Although this works, I am going to revise my answer. Hereβs why:
You cannot access the data files directly. For example, this:
{{ site.Data.guide.a.2021-06-04 }}
generates this error:
bad number syntax: ".2021-06"
And if you try to convince Hugo that the filename is a string by placing a non-numeric character at the beginning:
{{ site.Data.guide.a._2021-06-04 }}
it doesnβt like the hyphens:
bad character U+002D '-'
And if you try to use numbers onlyβ¦
{{ site.Data.guide.a.20210604 }}
it doesnβt like numbers:
unexpected ".20210604" in operand
So, use this insteadβ¦
Structure
data
βββ guide/
βββ a/
β βββ _20210607.json
βββ b/
βββ _20210601.json
layouts/_default/single.html
{{ with .Params.guide }}
{{ $data := index site.Data.guide .name (dateFormat "_20060102" .date) }}
{{ end }}
The front matter format remains unchanged.
2 Likes
@jmooring yes discovered this too
with the front matter like this now. I removed the date format file as this would have resulted in too many files being generated that dont need to be kept around so I just over write each day file now
guide:
name: "a"
page: "0"
data
βββ guide/
βββ a/
β βββ 0.json
βββ b/
βββ 0.json
1 Like
system
Closed
June 9, 2021, 4:42pm
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.