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 }}
bep
November 5, 2018, 1:54pm
3
Have you tested
{{ range where .Site.RegularPages ".Params.options.unlisted" true }}
???
bep
November 5, 2018, 1:56pm
4
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.
bep
November 5, 2018, 2:22pm
6
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.
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?
bep
November 6, 2018, 5:00pm
10
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 }}
bep
November 7, 2018, 3:53pm
12
Yes, which I suspect you can solve with an additional “where nil”.
budparr
November 7, 2018, 8:27pm
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.
bep
November 7, 2018, 9:13pm
15
I meant to post this:
I’m not sure why we fail on missing fields/map keys.
2 Likes