I am trying and failing to get a Section’s single template to inherit from the same base as its list template with Hugo v0.18.1. For some reason the ‘single.html’ template insists on inheriting from the default ‘baseof.html’ no matter what I’ve tried.
The ‘section/photographs.html’ inherits from ‘_default/photographs-baseof.html’ successfully, though ‘photographs/single.html’ inherits from ‘_default/baseof.html’ and not ‘_default/photographs-baseof.html’. How do I get ‘photographs/single.html’ to inherit from ‘photographs-baseof.html’?
This is driving me crazy and I have been working on it for quite a while now. I have tried moving ‘photographs-baseof.html’ into ‘sections/’ and ‘photographs/’ without success. Can someone help me understand how to get ‘section/photographs.html’ and ‘photographs/single.html’ to inherit from the same ‘photographs-baseof.html’? Am I misunderstanding how to organize things properly? Should I be using partials?
Any help would be deeply, deeply appreciated. Thank you.
My first thought would be to create a more flexible baseof.html that includes multiple blocks that can then be overwritten in the single template of your choice…
Thank you for the response. I removed all of my experimentation with this from the repo yesterday and therefore I don’t have any code to share. Though the directory structure above really is the problem boiled down to its simplest terms.
The other section, ‘photographs’ in the example, is essentially going to be a different site structurally and with a different stylesheet. Almost the only markup to be shared with the ‘parent’ site is the doctype. I was hoping to just use a different base for this, which seems to work fine for the list template—though not singles for some reason that I don’t understand.
I worked on this all day yesterday and have given up on blocks for this ‘subsite’. Using partials seems to be handling my needs perfectly though not as elegantly. I really like the block concept and will continue using it for the main part of the site.
Thanks again for the response, I appreciate the suggestion.
I am going to tag @bep on this one since I’m not sure whether you can have multiple base templates to inherit from. I still think that rather than have multiple base templates, you should use multiple blocks instead that contain the default content–then you can override them in individual templates. You also can do content views (essentially just an extension of partials).
Could you create an issue on GitHub to track this?
I will follow up on it, eventually; I just had a click look at the tests on this, and there aren’t any “single test cases”, and even though I suspected it should just work, you show that there may be some fishy going on here.
@ethet. Now that I’ve taken a better look at the docs, I’m taking a stab and saying this might have to do with the way you named your base templates…
I would think that photographs/single.html would inherit from _default/single-baseof.html rather than _default/photographs-baseof.html, since the latter is actually a list page and not a single page. Can you see if that works?
So I would think something like this might make more sense or work well for you (no idea without a repo):
photographs/single.html and things/single.html should inherit from _default/single-baseof.html.
section/photographs.html and section/things.html should inherit from section/baseof.html (and if there were no section/baseof.html, I believe both would inherit from _default/section-baseof.html and then _default/list-baseof.html).
@rdwatters The conditional block idea seems fraught to me, though if I found myself sharing any markup or CSS between the two (or more) ‘sites’: I would look into implementing the idea right away. The partials are working fine.
@bep I will build a small demo of the problem this evening and open an issue to demonstrate what words make difficult to explain. (On preview: Perhaps I am simply misunderstanding how to accomplish what I am trying to do.)
@rdwatters (On preview) My word… Looking at that structure gave me a headache
You are right: if I create _default/single-baseof.html, all of the singles from every section begin inheriting from that. What. This does not make any sense to me. If section/photographs.html (the ‘photographs’ list) inherits from _default/photographs-baseof.html, why doesn’t photographs/single.html (the ‘photographs’ single) inherit from _default/photographs-baseof.html?
How do I get the list and single of a section to inherit from the same baseof that is different from all other sections (their supposed photographs-baseof.html)? Perhaps that is the question I should be asking:
What is the proper way to organize a section’s base, list, and single templates so that the list and single both inherit from only that base for only that section?
Hmmm…so I guess I’ve never thought of your particular use case. I think the distinction between what you’re trying to do and what Hugo assumes you are trying to do—and Hugo assumes very little—is that you are treating folders in your content directory as separate sites, whereas Hugo assumes these folders are different sections within a single site, which is going to be a safe assumption in the overwhelming majority of uses cases.
This makes perfect sense to me since this is the way templating inherits via “blocks” or “extends” in the other templating languages I’ve used. This is more tethered to the typical hierarchical mental model of the web and the essence of URLs than it is to anything specific to Hugo, IMHO.
That said, every “single” template doesn’t have to inherit from_default/single-baseof.html if you don’t want it to. Remember that Hugo has a well-defined cascade of how it looks for templates when rendering the final output. So if you want any specific single.html layout to inherit from something other than the default template—and pay special attention to use of the word default here—simply put a single-baseof.html higher up in the cascade. Does that make sense?