Range where enigma

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

{{- $.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:

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

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

{{- $.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!

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

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

Another approach:

{{ range .Site.Taxonomies.categories.women }}
  {{ .Title }}<br>
{{ end }}
1 Like

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

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:
product = “products”

products: [“watches”]

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

Thank you in advance!

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:

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

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:

        <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 }}
              <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>
            {{- end }}
          {{- end }}
          {{- end }}

Content filter sample output:

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”]
categories : [“men”]

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

I’d like to use something like

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

but it crashes and throws in the error:

variable $name is not defined!

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

Thank you in advance!

minimalism works for me :slight_smile:

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.

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

categories = ["men"]

or women:

categories = ["women"]

or both:

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.

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.