Hugo

Accessing partial variables in template

I want to be able to use called partial’s variables in my template. Is there any way to do it?
For example, I have a pagination.html partial which only initializes $paginator. Now I want to be able to call $paginator after including pagination.html in my template using {{ partial "pagination.html" . }}.

You need to attach this pagination on your page context using .Scratch. Then as you are passing this page context to your pagination.html partial, you will be able to get it from there.

{{ .Scratch.Set "pagination" $paginator }}
{{ partial "pagination.html" . }}

Then from layouts/partial/pagination.html

{{ .Scratch.Get "pagination" }}

Realized you want to do that in the reversed order. But it’s the same. Use .Scratch.Set from within the partial, and then when using the page context outside the partial, you’ll be able to do a .Scratch.Get "pagination.