RegularPages - can't iterate over nil

I move this from a template (where it works just fine) into a partial

{{ range first 3 (where $.Site.RegularPages "Section" "posts") }}
{{ end }}

and it fails with error calling where: can't iterate over <nil>.
Why is that? I don’t see how the context would matter here.

Do you have a repo that shows this behaviour? I am unable to reproduce your error on a test.


Are you passing the “dot” on to the partial call?


{{ partial "foo.html" . }}


That is actually the big change to when it worked before. Because of the problems with relative links (see other thread) I am passing in a dict like this:

{{ partial "more.html" (dict "context" . "rel" "rel1") }}

but given that

{{ range $post := first 10 (where $.Site.RegularPages "Section" "posts") }}
{{ end }}

does not depend on . but only on $ I didn’t think that should matter.

I am working on stripped down repo to demonstrate it.

Here it is I run it with make run.
If you go into ./themes/tech/layouts/partials/more.html and add

{{ range $post := first 10 (where $.Site.RegularPages "Section" "posts") }}
{{ end }}

You will see the error.

Use instead:


Why does $self.Site.RegularPages work but not $.Site.RegularPages?
I thought $. should always work.
And why site and not .Site?

Thanks for the help!

From this bit in the docs:

$ is set to the starting value of . (“the dot”) by default.

My understanding is that when you are then inside a partial, the starting “dot” would be the context passed on to it. So in your case, the dict. You can see this by printing {{$}} / {{.}} from inside your partial.

$self.Site... works because $self is a Page, and therefore contains the .Site variable.

site.... works because site is a global variable, independent of context.

Seems like I totally misunderstood $. Sounds like it is not global but just per page/template/partial. I don’t find the docs too clear on that. And I didn’t even know there is site as global var. Thanks for clearing this up!