How to assign unique partials to specific content folders in Hugo?

Hi all. I’m currently working with Hugo and would like to know the best way to use different partials for different folders nested within the /content directory. Right now, Hugo is only rendering the header.html from the layouts/partials directory. However, I’d like to use a unique header.html for a specific content folder.

Partials do not have a lookup order. They are agnostic with respect to output format, page kind, page type, etc.

https://gohugo.io/templates/types/#partial

Unlike other template types, you cannot create partial templates to target a particular page kind, content type, section, language, or output format. Partial templates do not follow Hugo’s template lookup order.

layouts/
├── _default/
│   ├── baseof.html
│   ├── home.html
│   ├── list.html
│   └── single.html
├── partials/
│   └── header/
│       ├── books.html
│       ├── default.html
│       └── films.html
└── shortcodes/

Then in your template do something like:

...
  <header>
    {{ $partialPath := printf "partials/header/%s.html" .Section }}
    {{ if templates.Exists $partialPath }}
      {{ partial $partialPath }}
    {{ else }}
      {{ partial "partials/header/default.html" }}
    {{ end }}
  </header>
...
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.