Hello everyone) Please advise, is there any simple ready-made solution for my issue?
I need the variables in a partial template to correspond to the column names in a CSV table.
Currently, I have to create a separate variable for each column, but if there are 20-30 of them, it seems like too many.
Here is my CSV file for example:
place;name;money
1;macdonalds;10000
2;kfc;7000
3;burgerking;3000
Here is my template in index.html
{{ $data := dict }}
{{ $p := "/top.csv" }}
{{ with .Resources.GetMatch $p }}
{{ $opts := dict "delimiter" ";" }}
{{ $data = . | transform.Unmarshal $opts }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
{{/* skip first line */}}
{{ range after 1 $data }}
{{ with partial "top.html" . }}
{{ . }}
{{ end }}
{{ end }}
And this is how variables are currently declared in top.html:
{{$place := index . 0 }}
{{$name := index . 1 }}
{{$money := index . 2 }}
<div>
Place is: {{$place}}
</div>
<div>
Name is: {{$name}}
</div>
<div>
Money: {{$money}}
</div>
Everything seems to be working fine, but I wanted to know if there is a more elegant solution. Thank you in advance.
I think the most likely approach is to iterate over the first row of the CSV and assign the values of each column to variables. Then, further assign values to each variable. However, neither myself nor my little friend GPT seem to be able to figure this out