Insert partial via variable from range

Hi all!

Since yesterday, I have been trying to insert a partial via a range loop and have not been able to find a solution online. The partial to be inserted is a variable from a data file.

    {{ range .Site.Data.home_sections }}
    {{ partial .file . }}
    {{ end }}

When I add <p> tag for debugging purposes, the correct filename is output inside of the <p> tag. However, I don’t know in what form I need to pass this to the partial function, so it will be inserted.

    {{ range .Site.Data.home_sections }}
    <p>{{ .file }}</p>
    {{ partial .file . }}
    {{ end }}

I tried the following variants, but none of them did work:

    {{ range .Site.Data.home_sections }}
    {{ partial .file . }}
    {{ end }}

    {{ range .Site.Data.home_sections }}
    {{ partial ".file" . }}
    {{ end }}

    {{ range .Site.Data.home_sections }}
    {{ partial {{ .file }} . }}
    {{ end }}

    {{ range .Site.Data.home_sections }}
    {{ partial "{{ .file }}" . }}
    {{ end }}

This is the `home_sections.yml` File i use for definition of the sections:

- file: about.html

- file: projects.html

- file: about.html

- file: about.html

And this would be the manual way, I try to replace with a range. When doing like so, it works, but this isn’t the goal:

    {{ partial "about.html" . }}

    {{ partial "projects.html" . }}

    {{ partial "about.html" . }}

    {{ partial "about.html" . }}

The goal is to don’t hard code the order of the sections, so the user can change the order of the section in one place via yml (in the end via a headless cms).

Does anyone know why this is and whether it is even possible? Or is there even a better solution?

Thanks in advance and have a great day!

So, the above looks correct to me (assuming that the “.” is what you want to pass in).

You’re not telling me how it fails …? Do you get an error?

Thanks for the fast reply. With the mentioned way I don’t get an error but the partial also don’t gets inserted.

When doing like so:

    {{ range .Site.Data.home_sections }}
    <p>{{ .file }}</p>
    {{ partial .file . }}
    {{ end }}

The only thing inserted in the output is:

    
    <p>about.html</p>
    
    
    <p>projects.html</p>
    
    
    <p>about.html</p>
    
    
    <p>about.html</p>
    
    
    

No error, just this normal console output:

Change detected, rebuilding site (#1).
2026-01-11 12:47:06.839 +0100
Template changed /_default/baseof.html
WARN  found no layout file for "html" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN  found no layout file for "html" for kind "taxonomy": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Total in 4 ms

What do you mean by

I assume that the dot “.” is the current context or that it should be inserted at the current point. Manually it works with the dot, so I think this is what I want.

So I just created a GitHub Repo to create a empty hugo project showing the issue, but there it works.

Here you find the repo:

I’m currently unsure what the cause is and will compare my case with the provided repo. It seems to be a minor issue/typo/mistake in my config.

So, I just tried some things. It kinda looks like the problem has to do with the use of go templates inside of the partials.

I updated the repo to show the problem.

Any HTML inside of a go template like

{{ with .Site.GetPage "projects" }}
<p style="background: red;">And this isn't shown, when inserted via the range loop</p>
{{ end }}

Won’t be rendered when using a range loop like this:

    {{ range .Site.Data.home_sections }}
    <p>{{ .file }}</p>
    {{ partial .file . }}
    {{ end }}

An example below on the screenshot. The text with blue background is outside of the go template and always shown. The text with red background is inside the go template and not shown when rendered via range loop.

Do you know why this happens? Has it to do with the context of the partials.Include function? Do I use the wrong arguments?

looks like a context problem. as @bep mentioned with

assuming that the “.” is what you want to pass in

the dot inside the range points to the data file. if you want to pass the page you mostly want to use a dollar.

{{ partial .file $ }}

if you don’t need the page for more than .Site.GetPage you could use the global site function like so site.GetPage in your partial.

1 Like

Thanks. This was it.

I changed my go template to:

    {{ range .Site.Data.home_sections }}
        {{ partial .file $ }}
    {{ end }}

And now it works as expected.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.