Using where and in together

Hello,

I have a classification field in my set of md files which contains a sentence (many words) and then there are some md files where there is no classification (hence there is no content for classification on these).

Structure of MD files in content/fruits folder

fruit_code: "A001"
usage_name: "Apple"
scientific_name: "Malus pumila, Malus domestica, Malus sylvestris, Malus communis, Pyrus malus"
taste: ["sweet", "sour", "savoury"]
color: ["red", "green"]
pics: "fruits"
classification: "seeded, citrus"

When generating content, I would like to do the following -

  1. Find if the classification param is non empty
  2. When non-empty, I would like to search for a string like “citrus” or “berries” in the classification param and then
  3. Present the info in a grouped way such as -

citrus:
List of names of fruits that have citrus in their classification
berries:
List of names of fruits that have berries in their classification.

The above list needs to be alphabetically sorted, so I am using the sort as well.

I understand that this might not the best way to represent because there can be instances where a fruit might have more than one classification and my approach might not place the fruit name in other categories, but for now, I think I am okay with that (reason being I need a pointer on how to write my where statement and get things going).

This is what I have been trying (knowing that what I am writing is wrong - citrus would eventually need to be replaced with something more dynamic so that I can process more classifications like maybe use this inside a range or anything, just placing it here so that the community knows that I am trying, just not getting the right direction)

( sort ( in (where $.Site.Pages "Section" "fruits") "Params.classification" "citrus") ".Params.fruit_code"  )

Any suggestions would be welcome.

Thank you again for being my guides.
Sid.

I’d definitely use the range and where to filter out the content you want. Off the top something like

{{ $fruits := (where site.Pages "Section" "fruits" )}}
{{ $search := "citrus" }}
{{$filtered := where $fruits.Params.classification $search }}
{{range $filtered}}
  {{.Params.fruit_code}}
{{end}}

There is also groupBy with pages that make simple work of organizing and ranging through as well.

One error that I get into when I try compiling -

execute of template failed: template: fruits/index.html:106:51: executing "main" at <$signL.Params.classification>: can't evaluate field Params in type page.Pages

And another issue that I see is the fact that my classification field is not just one word. It is a comma separated phrase (not in ), hence merely searching for something like “citrus” might not get me all fruits with classification as “citrus”

And also, I am trying to get the fruits list sorted when displaying -

citrus:
List of fruits that have citrus in their classification sorted by "fruit_code" /*This fruit code will be hyperlinked to the actual fruit page*/

This is the reason I have been trying to include the “in” statement with the “where”. I am unsure if mine is the correct approach.

Whenever I see words like “classification” I immediately think about using Hugo’s taxonomy system.

If if were me, I would add this to my site config:

[taxonomies]
...
...
classification = 'classification'

And my front matter would look like this:

classification: ["seeded", "citrus"]

Then the list template would include something like:

{{ range $term, $weightedPages := site.Taxonomies.classification }}
  <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
  {{ range sort (where $weightedPages "Type" "fruits") "Title" }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a><br>
  {{ end }}
{{ end }}
4 Likes

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