Skip to content

How to pass multiple parameters to a partial in Hugo

New Course Coming Soon:

Get Really Good at Git

How do you pass multiple parameters to a partial in Hugo? It's not as simple as it seems, you need to use a trick. Let's find out.

I use Hugo to manage this site. It’s pretty cool.

One thing that got me stuck today was passing 2 parameters to a partial.

Since in a partial I could not access .Site.Pages to get the list of pages of the site (due to scope issues), I had to create a dictionary and fill it with 2 items:

{{ partial "my-partial.html" (dict "context" . "pages" $.Site.Pages) }}

The key here is passing (dict "context" . "pages" $.Site.Pages) as the parameter, instead of . as you usually do with partials.

Now inside the partial, instead of using . to access the current context variables you’d use .context.

And to access the value assigned to pages, I’d use .pages.

You can of course pass multiple items, too. Just add more items to the dict.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Go Handbook

Here is how can I help you: