How can I check multiple param values at once in range?

If my front matter has

---
param1: true
param2: false
param3: true
---

How can I check them all at once in range? For example this is fake code check if they are all false

{{ range where .Site.RegularPages ["param1", "param2", "param3"] false }}
{{ . }}
{{ end }}

I know about nested where but it seems it will become messy with many params to check at once

You have a couple of choices.

Nested where

{{ $p := where .Site.RegularPages "Params.param1" false }}
{{ $p = where $p "Params.param2" false }}
{{ $p = where $p "Params.param3" false }}
{{ range $p }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

Conditional

{{ range .Site.RegularPages }}
  {{ if not (or .Params.param1 .Params.param2 .Params.param3) }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}
4 Likes

Thank you for the help! What is the difference with := and = for the varables

Use := to initialize and use = to assign. See:
https://pkg.go.dev/text/template#hdr-Variables

1 Like

I was doing it wrong this whole time, thank you

What if I want to check if .Params.foo which is a slice has certain value instead of true or false? Still inside the where

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

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