3

I want to add a map to the main page of my blog. It needs to take a few parameters. I'm struggling to figure out how to configure this properly. I got it working by hard-coding the settings but that's not ideal for sharing my solution with others.

My problem is that I implemented it as a partial: {{ partial "map.html" (where site.RegularPages "Type" "in" site.Params.mainSections) }}

From my understanding I cannot access the .Site.Params variable in a partial. I've also been considering using a shortcode but that doesn't seem to be the right choice either because shortcodes can only be used in content, not in templates. I also don't want to add this into the index.html of the template directly as it is independent of the theme.

What is the correct way to achieve this?

2 Answers 2

6

.Site.Params == site.Params

see docs: https://gohugo.io/functions/site/#readout

That should resolve immediate issue. Otherwise: Smitop's solution.

(i.e. 'site' can be accessed globally)

4

You can pass the current context to a partial with .:

{{ partial "header.html" . }}

You can then access the context from within the partial as you would from the caller:

This site is called {{ .Site.Title }}.

Hugo doesn't directly support passing multiple arguments to a partial. If you want to pass things in addition to the context, you can pass a dictionary with all the values:

{{ partial "header.html" (dict "Ctx" . "Percent" "84") }}

And then from within the partial:

This site is called {{ .Ctx.Site.Title }}, and is {{ .Percent }}% awesome!
1
  • Thanks for the idea to use a dict. Solved my problem perfectly! Apr 16, 2023 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.