# Insert partial via variable from range

**URL:** <https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542>\
**Category:** support\
**Created:** [January 11, 2026, 9:49am UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542 "2026-01-11T09:49:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![michi](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@michi](https://discourse.gohugo.io/u/michi)\
**Post date:** [January 11, 2026, 9:49am UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/1 "2026-01-11T09:49:22Z")

</div>

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.

```xml
    {{ 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.

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

```

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

```xml
    {{ 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:

```yaml
- 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:

```xml
    {{ 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!

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 11, 2026, 11:25am UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/2 "2026-01-11T11:25:59Z")

</div>

> [@michi](#):
>
> ` {{ partial .file . }}`

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?

---

<div class="post-metadata">

**Author:** ![michi](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@michi](https://discourse.gohugo.io/u/michi)\
**Post date:** [January 11, 2026, 11:52am UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/3 "2026-01-11T11:52:44Z")

</div>

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:

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

```

The only thing inserted in the output is:

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

```

No error, just this normal console output:

```plaintext
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

> [@bep](#):
>
> assuming that the “.” is what you want to pass in

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.

---

<div class="post-metadata">

**Author:** ![michi](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@michi](https://discourse.gohugo.io/u/michi)\
**Post date:** [January 11, 2026, 12:18pm UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/4 "2026-01-11T12:18:49Z")

</div>

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:

> **[GitHub - michivonah/gohugo-partial-range-insert: Just showing how to insert a partial via a range...](https://github.com/michivonah/gohugo-partial-range-insert)**
>
> Just showing how to insert a partial via a range loop over a data yaml file in Hugo.

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.

---

<div class="post-metadata">

**Author:** ![michi](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@michi](https://discourse.gohugo.io/u/michi)\
**Post date:** [January 11, 2026, 12:54pm UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/5 "2026-01-11T12:54:52Z")

</div>

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.

> **[GitHub - michivonah/gohugo-partial-range-insert at...](https://github.com/michivonah/gohugo-partial-range-insert/tree/cd6ea4ed7de02a99a628ebd9d9db48595d3ca75c)**
>
> cd6ea4ed7de02a99a628ebd9d9db48595d3ca75c

Any HTML inside of a go template like

```auto
{{ 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:

```auto
    {{ 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.

 ![Screenshot_20260111_134914](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/8/2/827ac0bf5dfec708ff63184113ad0cd2d3e9d908.jpeg)

Do you know why this happens? Has it to do with the context of the [partials.Include](https://gohugo.io/functions/partials/include/) function? Do I use the wrong arguments?

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [January 11, 2026, 3:59pm UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/6 "2026-01-11T15:59:36Z")

</div>

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.

```auto
{{ 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.

---

<div class="post-metadata">

**Author:** ![michi](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@michi](https://discourse.gohugo.io/u/michi)\
**Post date:** [January 11, 2026, 4:27pm UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/7 "2026-01-11T16:27:23Z")

</div>

Thanks. This was it.

> [@irkode](#):
>
> the dot inside the range points to the data file. if you want to pass the page you mostly want to use a dollar.
> 
> ```auto
> {{ partial .file $ }}
> 
> ```

I changed my go template to:

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

```

And now it works as expected.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [January 13, 2026, 4:27pm UTC](https://discourse.gohugo.io/t/insert-partial-via-variable-from-range/56542/8 "2026-01-13T16:27:36Z")

</div>

Closed on January 13, 2026, 4:27pm UTC

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