Get regular pages matching params

The posts on my blog include the following in the metadata

options:
  showHeader: true
  unlisted: true

And I am trying to range over all the ones which are unlisted.

This so far hasn’t worked:

{{range (.Paginate (where (where $.Site.RegularPages.ByDate.Reverse "Type" "!=" "page") ($.Param "options.unlisted") true )  9).Pages }}
	{{ .Render "summary"}}
{{ end }}

What am I missing? Is there a simpler syntax I can use?

Well, this works, but feels messy:

{{ range where .Site.RegularPages ".Params.options" "!=" nil }}
   {{ if (.Param "options.unlisted") true }}
     	{{ .Render "summary"}}
   {{ end }}
{{ end }}

Have you tested

{{ range where .Site.RegularPages ".Params.options.unlisted" true }}

???

If you really need the nil check:

$pages :=  where.Site.RegularPages ".Params.options" "!=" nil
$pages = $pages | intersect (where $pages ".Params.options.unlisted" true )

I tried {{ range where .Site.RegularPages ".Params.options.unlisted" true }} first, but always get the error:

execute of template failed: template: unlisted/list.html:12:17: executing "main" at <where .Site.RegularP...>: error calling where: can't evaluate an invalid value

And no, I don’t really need the nil check, was just using it to narrow down the range a bit.

I think you will get rid of that error if you use my “intersect” construct above.

Yes it does work like you said. I do find it odd that the first version returns an error.

.Site.RegularPages ".Params.options.unlisted" true would be a much cleaner code to use.

It’s computers.

2 Likes

Alright, this just got weirder.

{{ range where .Site.RegularPages “.Params.options.unlisted” true }}

and

{{ range where .Site.RegularPages “.Params.options.unlisted” false }}

Both return the same results when the server is running and the site is rebuilt by a file change.

(nevermind this bit, it was another issue)

When I stop the server and run it again, it errors with the message: error calling where: can't evaluate an invalid value.

Should I file this as a bug on github?

Not unlsess you cannot provide better info than the above.

I figured out what was wrong. The query breaks when there is a .md file without an options.unlisted setting.
Otherwise, this works perfectly:

{{ range where .Site.RegularPages ".Params.options.unlisted" true }}

Yes, which I suspect you can solve with an additional “where nil”.

Sounds very much like the issue I was having here: Use where with nested fields? I was trying to avoid the second step, but ultimately did something like that.

I meant to post this:

I’m not sure why we fail on missing fields/map keys.

2 Likes