Where to place custom variables & overriding .IsPage

Let’s say I need to check if a page is .IsPage or .IsHome frequently.

Should I…

  1. Create a variable for it.
    {{ $isPageOrHome := or .IsPage .IsHome }}
    Where should I place this if I want all layout-files should have access to it?

  2. Is it possible to set IsPage to true on a .Kind “home”?

Thanks!

1 Like

The above condition does not quite make sense to me because the Homepage is a list page whereas a .IsPage kind refers to a regular page.

These different kind of pages i.e. lists and regular pages are governed by different templates.

In the end Hugo looks for templates in:

layouts/_default/single.html for regular pages
layouts/_default/list.html for list pages including the homepage (if the latter doesn’t have it’s own dedicated template.

Therefore I cannot think of a template in which you could put the condition you mention.

No.

Please have a look at the templates’ lookup order.

1 Like

@alexandros Thanks!

The above condition does not quite make sense to me because the Homepage is a list page whereas a .IsPage kind refers to a regular page.

In my case both the list pages and the regular pages use the same header- and footer-partial. And in those partials I do that conditional checking for adding different classes, meta description, canonicals etc that should only be applied if its a .IsPage or .IsHome.

I’m upgrading an older hugo installation and before we upgraded .IsPage was true on the home page so everything was fine and dandy. But now for some reason it’s not so I have to change the condition, unless we create different headers/footers for different page types, but that feels like overkill and a pain to maintain.

1 Like

Right. You need to use Base and Block Templates

Basically you will call the header and footer partials from within the /layouts/_default/baseof.html/ template and then you need to declare a block within the respective templates for the contents of list pages (like the homepage) and regular pages.

The first option in your original question is the way to go.

So from within your partials you can perform the following or similar check:

{{ $isPageOrHome := or .IsPage .IsHome }}
{{ with $isPageOrHome }}TRUE{{ else }}FALSE{{ end }}
1 Like

Great. Thanks for all your help. Would have taken me ages to go through all the docs.