Get regular pages matching params


#1

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?


#2

Well, this works, but feels messy:

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

#3

Have you tested

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

???


#4

If you really need the nil check:

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

#5

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.


#6

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


#7

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.


#8

It’s computers.


#9

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?


#10

Not unlsess you cannot provide better info than the above.


#11

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

#12

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


#13

#14

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.


#15

I meant to post this:

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