/

How to Pass Multiple Parameters to a Partial in Hugo

How to Pass Multiple Parameters to a Partial in Hugo

When working with Hugo, you may encounter the need to pass multiple parameters to a partial. While it may initially appear to be a simple task, there is actually a trick to accomplishing this. In this blog post, we will explore how to pass multiple parameters to a partial in Hugo.

First, it’s important to note that in a partial, you cannot directly access .Site.Pages to retrieve the list of pages on your site due to scope issues. To work around this limitation, you need to create a dictionary and populate it with the desired parameters.

Here’s an example of how you can pass two parameters, context and pages, to a partial:

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

In this code snippet, we are using (dict "context" . "pages" $.Site.Pages) as the parameter instead of just . as is typically done with partials. By passing a dictionary with the desired parameters, we can access them within the partial.

Inside the partial, you will now need to use .context instead of . to access the current context variables. If you want to access the value assigned to pages, you can use .pages.

And the great thing is that you can pass multiple items to the dict if needed. Just add more key-value pairs to the dictionary to include additional parameters.

By utilizing the technique of passing a dictionary to a partial, you can effectively pass multiple parameters and work around scope limitations in Hugo.

Tags: Hugo, partials, parameters, scope