Range where error message

This code

 {{- if isset .Site.Taxonomies "products" }}
 {{- if not (eq (len .Site.Taxonomies.products) 0) }}
 <ul class="list-unstyled">
   {{- range $name, $items := .Site.Taxonomies.products }}
   <li>
     <a class="cat-item d-lg-flex justify-content-between text-dark"
       href="{{ `products/` | relLangURL }}{{ $name | urlize | lower }}">{{ $name | title | humanize }}<span>{{len $items}}</span></a>
   </li>
   {{- end }}
 </ul>
 {{- end }}
 {{- end }}

ranges products from the search index index.json file

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
    {{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "categories" .Params.categories "contents" .Plain "permalink" .Permalink) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

I have following tags in watches.md product description file

# categories
categories : ["women"]

# products
products : ["watches"]

I am trying to put “where” condition into the range

{{- range where “categories” “women” $name, $items := .Site.Taxonomies.products }}

When I insert where into the range clause, I get the error message

undefined variable "$name"

What am I doing wrong? Guys, please help.

Thank you!

Your arguments are incorrect for where. See the docs for what arguments it expects.

I tried what you mean:
{{- range where .Pages "category" "women" $name, $items := .Site.Taxonomies.products }}
The error is the same: undefined variable “$name”

Hi

$name has to be defined -

example …

{{ $name = .Params.products }}

then you can do stuff like …

{{ range (where (index .Site.Taxonomies.products (lower $name)).Pages "Type" "in" site.Params.mainSections) }}

see the brackets around between where ( and the range )

1 Like