How to use partials for only certain pages

I’m starting with Hugo, and the block templates are a nice feature to have, altought I have some questions of something that I might be not understaing well.

I create a .html inside Partials folder that I want to use throughout the whole site. So let’s say I have:

{{ partial “my-new-partial” . }}

I cannot use this inside Content folder? So how can I use this template blocks in the whole site? Thank you!

Partials are commonly referenced from your layouts/_default/baseof.html. For example:

<!DOCTYPE html>
<html>
  {{- partial "head.html" . -}}
  <body>
    {{- partial "header.html" . -}}
    {{- block "main" . }}{{- end }}
    {{- partial "footer.html" . -}}
  </body>
</html>

Then, for example, in your layouts/index.html, you would define what goes into the main block:

{{- define "main" -}}
<!-- Some code here -->
{{- end -}}

Does that help?

Yes, that’s what I’ve been doing. But imagine I have a partial that I don’t want in X, Y and Z pages. If I already have on the baseof.html that partial, how can I remove in the X, Y and Z pages?

I see what you’re asking now. Well, there are a few way to do this. For example, say you only wanted to include a partial on pages of type post. In your baseof.html you would do:

{{ if eq .Type "post" }}
  {{- partial "some-partial.html" . -}}
{{ end }}
1 Like

For valid .Type values, see

You could also do a check on .Kind

That works!! Thank you!

1 Like

@prophet I changed your thread title from

Best way to add functions inside pages

To

How to use partials for only certain pages

For easier forum searching later on

1 Like