How to write theme-agnostic custom layouts?

I am wondering how best to write custom layouts so that they are agnostic to the theme; e.g. one can switch theme without a high likelihood of breaking the layout.

The main problem comes from the fact that there is no commonly practiced way to extend a layout. For instance, a custom layout probably wants to inherit some of the basic theme elements, like header, footer, and navbar.

Chances are this will work just by writing a custom layout with:

{{ partial "header.html" . }}
{{ partial "navbar.html" . }}

 < custom layout stuff here >
{{ partial "footer.html" . }}

But that’s making the somewhat strong assumption that the theme defines the header, navbar, and footer in partials with precisely those names, which not all themes will do.

It would be much more natural if themes defined some base layout with this stuff, and then the custom layout could inherit that base layout. (I see that Hugo does provide the ability to define base templates,, but most themes appear to define all layouts using partials alone and provide no base template.).

Any suggestions for handling this?

I assume this is because base/block template are a relatively new Hugo feature (perhaps v16?), and many of the themes were originally built or ported in earlier versions (or the theme devolepers just didn’t keep up with the new features). If you are making a theme, please use base templates and do not construct your URLs using the baseURL value in your config…

I’ve been working with Hugo themes for a couple of weeks, having years of WordPress theming behind me, so I definitely feel that impulse to have a pre-defined set of hooks to build around. On the other hand, the flexibility is incredible, because I can easily follow a whim without looking up a function…

I’ll have to read the base docs again, because they only seemed to matter for custom outputs, but from this discussion I can see them being used as a base of a theme family or system. Hugo 1.0 is likely far away, so I don’t think we’ve had a lot of discussion about this yet.

I’m wondering if we’ll create a best practice spec for the use of partials, similar to the Theme Hook Alliance for WordPress. Despite the name, it is not a piracy initiative. :slight_smile:

@rdwatters do you know of a theme that is using base templates?