Searching for an item in a List

Hello again,

Past few days I have been reading, and coding which is somehow good for me. And the best is that I hop on to this forum, search and if not found, ask. And thank you community for being there to help me find my way out.

So here is what I am trying to do -

I have a folder under content called fruits. This folder has files like apple.md, banana.md - basically fruitname.md for the 50 odd files I have here.

The following is the front-matter that I have - example from my apple.md file :

Edits to the front-matter after @jmooring pointed out the errors.

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

Now, I have a problem. It might be easily resolvable if I used Tags (color and taste could be tags as far as I have understood GoHugo), but I do not want to have it done that way. Instead, I want to be able to search for fruits using a color.

So:
If I want to find fruits that are green in color, I need to search through all MD files in fruits folder (content/fruits/) locate the field/parameter “color” and see if this list contains “green”. If so, I print the details of this fruit.

Here is where my code fails (going by the error -

wrong number of args for in: want 2 got 4

I know I should pass an array or a slice to IN but I am unable to figure out the how) -

$selcolor := "green"
$listfruits = in (where $site.Pages "section" "fruits") "Params.color" "=" $selcolor

I hope this is doable with goHugo and if so how do I get the “in” working here? This is important for me to learn because the next thing I want to do the same with the taste.

Thank you for reading

I assume this is a typo, and should be:

taste: ["sweet", "sour", "savoury"]

Also, use underscores instead of hyphens when naming keys. So this:

usage_name: "Apple"
scientific_name: "Malus pumila, Malus domestica, Malus sylvestris, Malus communis, Pyrus malus"
taste: ["sweet", "sour", "savoury"]
color: ["red", "green"]
pics: "fruits"
1 Like

@jmooring

Thank you. I have updated my post above and corrected the typo.

taste: ["sweet", "sour", "savoury"]

See https://gohugo.io/functions/where/#use-where-with-intersect

{{ $selcolor := "green" }}
{{ $pages := where site.RegularPages "Type" "fruits" }}
{{ $pages = where $pages "Params.color" "intersect" (slice $selcolor) }}
{{ range $pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Params.usage_name }}</a></h2>
{{ end }}

Or with nested where statements:

{{ $selcolor := "green" }}
{{ range where (where site.RegularPages "Type" "fruits") "Params.color" "intersect" (slice $selcolor) }}
  <h2><a href="{{ .RelPermalink }}">{{ .Params.usage_name }}</a></h2>
{{ end }}
4 Likes

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