steppa
December 31, 2022, 8:17pm
1
How do I get the correct path in this file (named health.json) to get the fields under properties
to work? Doing index site.Data.health.features.properties
throws an error execute of template failed at <site>: can’t evaluate field properties in type interface {}
.
{
"type": "Collection",
"crs": { "type": "name", "properties": { "name": "name" } },
"features": [
{
"type": "Feature",
"id": 0,
"geometry": { "type": "Point", "coordinates": [23, -3] },
"properties": {
"FID": 0,
"OBJECTID": 1,
"Facility_N": " Dispensary",
"Type": "Dispensary",
}
}],
}
bep
December 31, 2022, 8:27pm
2
features
is an array so you need to select one, e.g:
{{ $first := index site.Data.health.features 0 }}
{{ range $first.properties }}
...
{{{ end }}
steppa
December 31, 2022, 8:39pm
3
Now gettimg this error execute of template failed at <.FID>: can’t evaluate field FID in type interface {}
.
{{ $first := index site.Data.health.features 0 }}
{{ range $first.properties }}
{{ .FID }}
{{ end }}
FID isn’t part of the context in a range iteration.
If you want to get the FID:
{{ (index site.Data.health.features 0).properties.FID }}
steppa
December 31, 2022, 9:25pm
5
Almost works (I am callin multiple fields under properties
). Since the part below is recurring, how to ensure all parts appear? Right now only one item appears (with all its fields under properties
), which I assume is positioned as 0
.
{
"type": "Feature",
"id": 0,
"geometry": { "type": "Point", "coordinates": [23, -3] },
"properties": {
"FID": 0,
"OBJECTID": 1,
"Facility_N": " Dispensary",
"Type": "Dispensary",
}
I reworked my code
<ul>
{{ with (index site.Data.health.features 0).properties }}
<li>{{ .FID }}</li>
<li>{{ .OBJECTID }}</li>
<li>{{ .Facility_N }}</li>
<li>{{ .Type }}</li>
...
{{- end }}
</ul>
That wasn’t obvious from your initial example.
features
is an array, so you need to range over it.
{{ range site.Data.health.features }}
{{ with .properties }}
<ul>
<li>{{ .FID }}</li>
<li>{{ .OBJECTID }}</li>
<li>{{ .Facility_N }}</li>
<li>{{ .Type }}</li>
</ul>
{{ end }}
{{ end }}
system
Closed
January 2, 2023, 10:36pm
8
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.