Hello,
Problem 1
There seems to be a bug in how TOML works. This is what I see.
If I put a toml file in the data folder, its shortcode in the layouts/shortcodes folder, and the call in the markdown file, then change someting in the toml file, the page is updated on the fly. Seems ideal, as the code is short and the update is fast.
If I add the same using csv, hugo returns an error as soon as I add the call in the markdown file. What happens is that the toml shortcode wants to read all files in the data folder, and triggers an error for anything other than toml and yml.
This is the shortcode for toml:
{{ $data := .Site.Data.filename }}
<ul>
{{ range $data.filename }}
<li>
<strong>Name:</strong> {{ .name }}<br>
<strong>Email:</strong> {{ .email }}
</li>
{{ end }}
</ul>
The error code is:
unexpected data type [][]string in file filename.csv logged 1 error(s)
This makes it impossible to work with mixed file formats other than toml and yml.
Problem 2
If I remove the toml call from the markdown file, the csv chain works as expected. However, unlike with toml, if I change something in the csv file, the page is not updated on the fly. To update the page, I have to restart hugo.
Problem 3
Suppose you have 100 markdown files. Each file is like a headed letter: the name and address of the company is printed center front. You do not want to write name and address 100 times. In Jekyll (competitor), you write the following inline code, directly in the markdown file, and it reads data/filename.csv automatically:
<center>
{{ site.data.filename[0].name }}<br>
{{ site.data.filename[0].address }}
</center>
In Hugo, you have to write a shortcode, then call the shortcode from the markdown file.
The shortest shortcode (pun intended) in go is the above one, which requires toml. Other file formats require more code.
{{ $csvData := readFile "data/filename.csv" }}
{{ $lines := split $csvData "\n" }}
{{ if gt (len $lines) 1 }} <!-- Check if there is at least one data row -->
{{ $header := split (index $lines 0) "," }} <!-- Get the header row -->
{{ $firstLine := index $lines 1 }} <!-- Get the first data row -->
{{ $fields := split $firstLine "," }}
{{ $company := dict }} <!-- Create a dictionary to hold the company data -->
{{ range $index, $name := $header }}
{{ $key := trim $name "\"" }} <!-- Remove quotes from the header name -->
{{ $value := trim (index $fields $index) "\"" }} <!-- Remove quotes from the field value -->
{{ $company = $company | merge (dict $key $value) }} <!-- Add to the company dictionary -->
{{ end }}
<div class="company">
<p>{{ printf "%s, %s" $company.name $company.email }}</p> <!-- Output name and email -->
</div>
{{ end }}
And it still fails, because it returns a piece of the address instead of the email.
Problem 4
Assume you solved the above problems.
Since you have 100 pages, will Hugo read data/filename.csv 100 times?
Problem 5
Suppose you have a markdown page with sprinkled data from the csv file. With Jekyll you focus on the markdown file and write the occasional {{ site.data.filename[0].wantedfield }} where it belongs.
With Hugo, you have to write it all in the shortcode.
I am learning Hugo, because Jekyll is a nightmare to install, and is slow to update. So this is a one way journey for me. Can you point me to the right direction please, as the above problems are a slowing me down.
Thank you.