I’m having an issue trying to range a local csv file, I’m using the docs example and I keep getting this error in the console:
ERROR 2018/05/23 16:25:07 Error while rendering "page" in "company/": template: theme/_default/team.html:54:37: executing "theme/_default/team.html" at<getCSV "," "/csv/tes...>: error calling getCSV: Cannot find separator , in CSV.
This is what my code looks like:
{{ $url := "/csv/test.csv" }}
{{ $sep := "," }}
{{ range $i, $r := getCSV $sep $url }}
<div>Name: {{ index $r 0 }} - Title: {{ index $r 1 }}</div>
{{ end }}
My csv file is located in: themes/theme_name/static/csv/text.csv
The content of my csv file:
Name,Title
Benjamin Marte,Developer
Charles Burgess,Developer
Chris Leyva,Designer
If I render out the $url
in a a tag I am able to download the csv file so hugo should be able to read from it.
Any help figuring out what I am doing wrong would be greatly appreciated, thanks.
1 Like
benmarte:
The content of my csv file:
Name,Title
Benjamin Marte,Developer
Charles Burgess,Developer
Chris Leyva,Designer
If that really is the content of your CSV, it isn’t valid. Any entry with a space in it must be surrounded by double-quote marks.
So I updated my CSV to this and I still get the error :-/
"FirstName","LastName","Title"
"Ben","Marte","Developer"
"Charles","Burgess","Developer"
"Chris","Leyva","Designer"
Thanks
OK, next I would suggest trying to dump the whole file out by stripping everything back to just:
<pre>
{{ (getCSV "," "/csv/test.csv") | safeHTML }}
</pre>
Or similar. To see if you still get the error or get some valid output.
Ok I finally solved the issue, the problem was it was not loading the csv file even though the link was correct so I used printf
to concatenate the file path with thee .Site.BaseURL
and it works now.
Final code for reference:
{{$csv := printf "%scsv/test.csv" .Site.BaseURL}}
{{$sep := ","}}
{{range $i, $r := getCSV $sep $csv}}
{{if gt $i 0}}
<p>Name: {{index $r 0}} {{index $r 1}}</p>
<p>Title: {{index $r 2}}</p>
{{end}}
{{end}}
Thanks for your help @TotallyInformation
2 Likes