martin
martin

Reputation: 2194

Read site configuration from a Hugo partial

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?

Upvotes: 3

Views: 1783

Answers (2)

Rogelio
Rogelio

Reputation: 1046

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

Upvotes: 8

loops
loops

Reputation: 5635

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!

Upvotes: 4

Related Questions