As a result, I need to define main_header_content in the layouts where I want the header displayed; and add {{ define "main_header" }}{{ end }} in all the layouts where I don’t want the div to be added, because there is no main_header.
Is there any trick to avoid creating main_header.html and have the div defined in the base layout?
I think you can do what you want, without blocks. How about making the partial conditional, and including everything inside the partial (such as the div)?
I’d like to avoid defining the partial, in order to have the general layout defined in the baseof.html file. Partials are should be used to define content constrained by the general layout.
Then, in some sections (and the single pages in them) I don’t want main_header. In others, I want it shown in the single pages, but not in the pages list. Last, sometimes I want it shown in the list and page layouts.
How complex is your site? Are we talking about lots of sections and thousands of content pages?
If yes, you should stick with what you have.
But if your site is not that big you could assign a front matter parameter in content files, sections’ _index.md and pages where you want the main.header displayed. Then you can render it only on those pages using a simple with function based on that front matter parameter.