[SOLVED] Error calling 'where': can't iterate over <nil> (Solution: put a dot as the second argument for 'partial')

If I use {{ partial "recentposts" }} in layouts/index.html, I get this error:

 Template changed "/home/rea/works/theblog/layouts/index.html": CREATE
 ERROR 2017/12/06 12:48:35 Error while rendering "home": template: index.html:26:3: executing "index.html" at <partial "recentposts">: error calling partial: template: partials/recentposts.html:13:48: executing "partials/recentposts.html" at <where .Site.RegularP...>: error calling where: can't iterate over <nil>

And this is contents of the partial/recentposts.html file:

{{ range first 4 (where (where .Site.RegularPages "Section" "blog") "Params.show" "true") }}
  <h4><a href="{{ .Permalink }}" title="">{{ .Title }}</a></h4>
  {{ .Summary }}
{{ end }}

‌ ‌
‌ ‌
If I put the recentposts.html contents directly whitin index.html, it works well! Why the ‘partial ing’ leads to the error?

You’re probably just missing the dot. See https://gohugo.io/templates/partials/.

One of the most common mistakes with new Hugo users is failing to pass a context to the partial call. In the pattern above, note how “the dot” (.) is required as the second argument to give the partial context. You can read more about “the dot” in the Hugo templating introduction.

1 Like