Range filtering data read from /data files

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?

{{ with index site.Data.services (.Get 0) }}
  {{ .name }}
  {{ range .services }}
    {{ . }}
  {{ end }}
{{ end }}
1 Like

That works; to work through what’s going on here:

index is a Hugo function. index | Hugo

index COLLECTION INDEXES

where the data is the collection, and the shortcode param is the index. The data (collection) is an aggregation of files in the directory, and the function return value corresponds to the content of one of those files.

Much appreciated.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.