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 }}
        <p class="sl-offers__subtitle">
            {{ .Params.offers.subtitle | safeHTML }}
<ul class="sl-offers__list container">
        {{ range $section.Pages }}
                <a class="offer" href="#">
                    <div class="offer__img">
                        <img src="/images/offers/{{ .Params.icon }}" alt=""/>
                    <p class="offer__title">{{ .Title }}</p>
                    <span class="offer__subtitle">{{ .Params.duration }}</span>
                    <p class="offer__description">{{ .Description }}</p>
        {{ end }}

My simplified file :

  title: Nos offres
  subtitle: Exemple de sous-titre

I need to get data from :

  • my content/fr/ and content/en/ files
  • my offer pages in content/fr/offers/ and content/en/offers/

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/ and content/fr/
content/fr/offers/ and content/en/offers/


content/fr/offers/ and content/en/
content/fr/offers/ and content/en/offers/

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 and my, in their respective language versions.

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

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?

β”œβ”€β”€ en
β”‚   β”œβ”€β”€ offers
β”‚   β”‚   β”œβ”€β”€
β”‚   β”‚   β”œβ”€β”€
β”‚   β”‚   └──
β”‚   └──
└── fr
    β”œβ”€β”€ offers
    β”‚   β”œβ”€β”€
    β”‚   β”œβ”€β”€
    β”‚   └──

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 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.