How can I pass the correct context to my partials

In my index.html file, i have :

{{- partial "homepage/offers.html" . -}}

Simplified offer.html :

{{ $section := site.GetPage "offers" }}
<section class="sl-offers">
    <div class="container flex flex-column justify-center align-center">
        <h2 class="h2 sl-offers__title">
            {{ .Params.offers.title | safeHTML }}
        </h2>
        <p class="sl-offers__subtitle">
            {{ .Params.offers.subtitle | safeHTML }}
        </p>
    </div>
<ul class="sl-offers__list container">
        {{ range $section.Pages }}
            <li>
                <a class="offer" href="#">
                    <div class="offer__img">
                        <img src="/images/offers/{{ .Params.icon }}" alt=""/>
                    </div>
                    <p class="offer__title">{{ .Title }}</p>
                    <span class="offer__subtitle">{{ .Params.duration }}</span>
                    <p class="offer__description">{{ .Description }}</p>
                </a>
            </li>
        {{ end }}
    </ul>
</section>

My simplified _index.md file :

---
offers:
  title: Nos offres
  subtitle: Exemple de sous-titre
---

I need to get data from :

  • my content/fr/_index.md and content/en/_index.md files
  • my offer pages in content/fr/offers/page-1.md and content/en/offers/page-1.md

I know this is a context issue but I can’t get the syntax right from the docs.

Thank you for your help

Do you mean you need to get content from:

content/fr/_index.md and content/fr/_index.md
content/fr/offers/page-1.md and content/en/offers/page-1.md

or

content/fr/offers/_index.md and content/en/offers_index.md
content/fr/offers/page-1.md and content/en/offers/page-1.md

Which one is correct?

Sorry, it wasn’t clear indeed as I left a typo. I edited my original message.

To be 100% clear, I need data from both _index.md and my offer-1.md, in their respective language versions.

That’s your home page. Are you sure you don’t mean content/fr/offers/_index.md?

That is indeed my homepage !

I am able to display the data from this .md file using this syntax :

{{- partial "homepage/offers.html" . -}} using the . to pass context in my index.html file.

{{ .Params.offers.title | safeHTML }} and accessing the data thanks to the .Params variable, in my offers partial.

But when i’m using this syntax, I’m not able to display the data from β€œoffers”, which is in my $section variable : {{ $section := site.GetPage "offers" }}, because the .Params variables already refers to something else, I guess?

Sorry if it’s still not clear, i’ll be happy to provide other details if needed.

Is this your site structure?

content
β”œβ”€β”€ en
β”‚   β”œβ”€β”€ offers
β”‚   β”‚   β”œβ”€β”€ offers-1.md
β”‚   β”‚   β”œβ”€β”€ offers-2.md
β”‚   β”‚   └── offers-3.md
β”‚   └── _index.md
└── fr
    β”œβ”€β”€ offers
    β”‚   β”œβ”€β”€ offers-1.md
    β”‚   β”œβ”€β”€ offers-2.md
    β”‚   └── offers-3.md
    └── _index.md

Please confirm.

Yes it is.

Everything is working for me. I must be doing something wrong. Try this:

git clone --single-branch -b hugo-forum-topic-31847 https://github.com/jmooring/hugo-testing hugo-forum-topic-31847
cd hugo-forum-topic-31847
hugo server
1 Like

You reproduced perfectly my setup : I went and pasted everything from your files just to be sure to not miss anything, and after doing that it just worked on my project too, though I’m not even sure what was the problem about.

I think I got things mixed up while trying different solutions and did not fully revert to a clean state.

Thank you so much for your time and patience.

You call the partial from your home page layout, and the home page looks fine.

Were you calling the partial from another layout too?

That is the only layout I am calling the partial from. Before trying that I was trying to pass variables like this : {{- partial "homepage/offers.html" (dict "context" . "pages" $.Site.Pages) -}} when in fact it was much simpler, like I suspected.

My guess is that I probably forgot the β€œ.” in my code like this : {{- partial "homepage/offers.html" -}} but somehow added it when I simplified the file here on the topic…

1 Like

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