kkvero
November 12, 2024, 1:09pm
1
For example, to read from yaml front matter this list:
tags: ["a", "b"]
It works to use in index.json
:
"tags":
{{- with .Params.tags -}}
{{ delimit . ", " | jsonify }}
{{- else -}}
""
{{- end -}}
Result:
"tags":"a, b"
My attempt to read:
technologies: [["a1", "b1", "c1"], ["a2", "b2", "c2"]]
with:
"technologies":
{{- with .Params.technologies -}}
{{range . }}
{{ delimit . ", " }}
{{end}}
{{- else -}}
""
{{- end -}}
Gives:
"technologies":
a1, b1, c1
a2, b2, c2
I would be satisfied with:
"technologies": "a1, b1, c1, a2, b2, c2"
Wrapping in another range gave gibberish numbers between the lines.
Any hints are welcomed!
irkode
November 12, 2024, 1:40pm
2
You have nested lists so you could using append to flatten the list and then delimit it:
{{ with .Params.Technologies }}
{{ $merged := slice }}
{{ range . }}
{{ $merged = $merged | append .}}
{{ end }}
<!-- direct output to page -->
"technologies": "{{ delimit $merged ", " }}"</br>
<!-- or create json string -->
{{ $object := jsonify (dict "technologies" (delimit $merged ", ")) }}
<!-- and render it -->
{{ highlight $object "JSON"}}
{{end}}
1 Like
kkvero
November 12, 2024, 2:45pm
3
Neet! It worked, thank you!
I had to place “technologies” above everything:
"technologies":
{{ with .Params.technologies }}
{{ $merged := slice }}
{{range . }}
{{ $merged = $merged | append .}}
{{end}}
{{ delimit $merged ", " | jsonify}}
{{- else -}}
""
{{end}}
Otherwise got json parse error, when page has no “technologies” json output: "tags":"..",
So he minds the trailing comma. But this is ok: "tags":"", technologies:""
system
Closed
November 14, 2024, 2:45pm
4
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.