Need help understanding .Kind contexts

I’m struggling to understand how the value of the .Kind variable changes as partials get included.

Consider the following layouts/index.html file:

{{- partial "header.html" . }}
{{- range .Paginator.Pages }}
{{- partial "content.html" . }}
{{- end }}
{{- partial "pagination.html" . }}
{{- partialCached "footer.html" . }}

If I stick a simple {{ .Kind }} in this file, it will display “home”.

But if I stick the same {{ .Kind }} inside the contents.html partial, it will display “page”.

Is this correct? Why would the .Kind change as part of the home page is generated?

Go templates are contextually aware, so when you pass “the dot” into a partial, you’re passing in the context of the rendered page. Therefore, the value of .Kind changes according to the kind of page on which it is rendered…

So the context is changing from “home” to “page” when Go renders the partial content.html? That implies to me that every partial will always override the context of the outer page that includes that partial. Is that correct?

No.

If the outer page is home below:

{{- partial “header.html” . }} <= home
{{- range .Paginator.Pages }}
{{- partial “content.html” . }} <= most likely a regular page (I say most likely because, by default, sections etc. will be in this list)
{{- end }}
{{- partial “pagination.html” . }} <= home
{{- partialCached “footer.html” . }} <= home

So, the “.” you pass down to the partial determines the output of .Kind from that partial.

1 Like