# Range where enigma

**URL:** <https://discourse.gohugo.io/t/range-where-enigma/26619>\
**Category:** Uncategorized\
**Created:** [June 30, 2020, 5:41pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619 "2020-06-30T17:41:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vistad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vistad/32/20509_2.png) [@vistad](https://discourse.gohugo.io/u/vistad)\
**Post date:** [June 30, 2020, 5:41pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/1 "2020-06-30T17:41:26Z")

</div>

Hello guys! This is a well known code that builds an index.json file. It works fine.

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

```

This is the Where definition:  
{{ range where .Pages “Section” “foo” }}  
{{ .Content }}  
{{ end }}

This is /contents/women/product.md file I have:

```auto
---
title: "Product"
date: 2019-11-25T13:39:07+06:00
draft: false
categories: ["women"]
etc.
---

```

The index.json code stops working after I add where clause:

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

```

What’s wrong??  
Thank you!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 30, 2020, 6:30pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/2 "2020-06-30T18:30:54Z")

</div>

You are comparing a slice to a string. Try this:

```auto
{{ range where .Site.RegularPages ".Params.categories" "intersect" (slice "women") }}
  {{ .Title }}<br>
{{ end }}

```

Another approach:

```auto
{{ range .Site.Taxonomies.categories.women }}
  {{ .Title }}<br>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![vistad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vistad/32/20509_2.png) [@vistad](https://discourse.gohugo.io/u/vistad)\
**Post date:** [June 30, 2020, 6:53pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/3 "2020-06-30T18:53:24Z")

</div>

O, thank you!!! Exactly! The first approach worked! After things come to the worst they will inevitably start changing to the better. 🙂

May be you can understand what’s going on here?  
{{ range $name, $items := .Site.Taxonomies.products }}

It crashes if I add where clause  
{{ range where .Pages “categories” “women” $name, $items := .Site.Taxonomies.products }}  
with error message ‘undefined variable “$name”’

Taxonomies and products (.md file) look like this:  
[taxonomies]  
product = “products”

products: [“watches”]

I tried comparing it as a slice - it’s not enough.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 30, 2020, 8:27pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/4 "2020-06-30T20:27:55Z")

</div>

First, it is not clear to me what you have or what you want.

Second, `.Site.Taxonomies.products` is a page collection. Insert the following in your code to see what it contains:

```auto
<pre>{{ jsonify (dict "indent" " ") .Site.Taxonomies.products }}</pre>

```

---

<div class="post-metadata">

**Author:** ![vistad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vistad/32/20509_2.png) [@vistad](https://discourse.gohugo.io/u/vistad)\
**Post date:** [June 30, 2020, 9:08pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/5 "2020-06-30T21:08:59Z")

</div>

I have reformulated my task at hand in a clearer way. This is a content filter - it reads product-description.md files in /content folder and subfolders. I have product-description.md files in two subfolders /content/women and /content/men.

If I open /women page - Hugo uses /layouts/\_default/list.html to list content from /content/women folder, if /men - from /content/men folder. Content filter code (below) is located on the list.html page.

I want the content filter code to determine if list.html is opened as /women or /men and to list content only from this folder.

Here is the content filter code that lists products and enumerates them:

```auto
        <section class="widget widget-products mb-5">
          <h3 class="widget-title mb-4">Products</h3>
          {{- 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 }}
        </section>

```

Content filter sample output:

Products  
Bags 4  
Jeans 2  
Pants 1  
Shirts 1  
Shoes 5  
Suits 1  
T shirts 1  
Tops 3  
Watches 2

It searches through product-description.md files in /content/women/ and /content/men/ folders that contain tags:

categories : [“women”]  
or  
categories : [“men”]

products : [“bags”]  
or  
products : [“shoes”]

I’d like to use something like

```auto
{{- range where .RegularPages "category" "women" $name, $items := .Site.Taxonomies.products }}

```

but it crashes and throws in the error:

```auto
variable $name is not defined!

```

Please help me if you can understand what’s going on here!

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![damien1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/damien1/32/11594_2.png) [@damien1](https://discourse.gohugo.io/u/damien1)\
**Post date:** [July 1, 2020, 1:36pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/6 "2020-07-01T13:36:15Z")

</div>

> [@jmooring](#):
>
> ```auto
> {{ range .Site.Taxonomies.categories.women }}
> {{ .Title }}<br>
> {{ end }}
> 
> ```

minimalism works for me 🙂

---

<div class="post-metadata">

**Author:** ![vistad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vistad/32/20509_2.png) [@vistad](https://discourse.gohugo.io/u/vistad)\
**Post date:** [July 1, 2020, 1:45pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/7 "2020-07-01T13:45:54Z")

</div>

Great, thank you for looking into this! So, the second method worked for you. It didn’t work for me - but the 1st one worked for me. Now it would be great if you can think about the content filter “range where”. thank you.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 1, 2020, 2:33pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/8 "2020-07-01T14:33:13Z")

</div>

I understand why you have used a taxonomy for categories. A product could be intended for men:

```auto
categories = ["men"]

```

or women:

```auto
categories = ["women"]

```

or both:

```auto
categories = ["men", "women"]

```

A product, on the other hand, is either a Bag or a Watch; it cannot be both. Our lives would be easier if we could use a simple string in front matter to classify the product, instead of a taxonomy term. If we can use a simple string, we will be able to use GroupByParam to count the number of each class of women’s products.

---

<div class="post-metadata">

**Author:** ![vistad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vistad/32/20509_2.png) [@vistad](https://discourse.gohugo.io/u/vistad)\
**Post date:** [July 1, 2020, 3:04pm UTC](https://discourse.gohugo.io/t/range-where-enigma/26619/9 "2020-07-01T15:04:39Z")

</div>

Exactly. The category defines one or more product groups, while a product is either this or that. I could not find a way to rewrite this expresson to do the taks - I hope you can.
