Skip to content

How to pass multiple parameters to a partial in Hugo

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.


→ Get my Go Handbook

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Related posts that talk about hugo: