The question is about how to filter data from the context of (shortcode) html:
Given a set of JSON files in a directory /data/services
/data/services/one.json
{
"name": "one",
"services" :
[
"alpha",
"beta",
"gamma"
]
}
/data/services/two.json
{
"name": "two",
"services" :
[
"delta",
"epsilon",
"zeta"
]
}
/data/services/three.json
{
"name": "three",
"services" :
[
"eta",
"theta",
"iota"
]
}
This shortcode correctly renders the full set of services from the 3 json files
/layouts/shortcodes/services.html
<div>
{{ range .Site.Data.services }}
<div>{{ .name }}</div>
{{ range .services }}
<div>{{ . }}</div>
{{ end }}
{{ end }}
</div>
Now modify the shortcode to pass a parameter to use for filtering:
{{ $group := ( .Get 0) }}
<div>
{{ range .Site.Data.services }}
<p>{{ .name }} {{ $group }}</p>
<div>{{ .name }}</div>
{{ range .services }}
<div>{{ . }}</div>
{{ end }}
{{ end }}
</div>
And the shortcode is called as:
{{< services "two" >}}
How to modify the shortcode so that only services from group “two” are rendered?
The debug line:
<p>{{ .name }} {{ $group }}</p>
prints
one two
two two
three two
So .name
and the shortcode param $group
are the same, but how to use that to filter the range? I’m thinking it should be:
{{ range where .Site.Data.services .name $group }}
…but that is not valid syntax. How would you filter the data to a specified group in this context?