Empty define doesn't override block. Should it?

Hi Hugoians.

I have my blocks sets in baseof.html and in a specific section list.html I don’t want the default block’s contents and I don’t want anything to replace it either: I just want nothing. So I thought I might be able to override the default block and exclude the partial it calls by defining an empty block in list.html. But that doesn’t work — a defined empty block doesn’t override the default block.

<body class=“baseof ma0 {{ with getenv “HUGO_ENV” }} {{ . }}{{ end }} {{ with .IsHome }}home{{ end }}{{ with .IsPage }}page{{ end }}”>
{{ block “env” . }}{{ partialCached “env” . }}{{ end }}
{{ block “header” . }}{{ partialCached “header” . }}{{ end }}
{{ block “usedby” . }}{{ partialCached “usedby” . }}{{ end }}
{{ block “main” . }}{{ end }}
{{ block “footer” . }}{{ partialCached “footer” . }}{{ end }}
{{ block “scripts” . }}{{ partialCached “scripts” . }}{{ end }}

Adding a html comment to the define block does work — nothing is rendered _ but I’m guessing there’s a much better approach to this scenario and if anyone cares to share I’m all ears.

{{ define “usedby” }}{{ end }} // An empty define doesn’t override the default block
{{ define “usedby” }}[HTML comment here]{{ end }} // Adding a HTML comment does override the default block and displays no content


I would recommend setting the blocks to empty at the top level and then defining them when needed within individual templates. Blocks are more similar to an extends mechanism; i.e., I think it’s easier to grock the idea of them adding rather than deleting, IMHO.


Thanks @rdwatters. 95% of my templates need the block so it feels dirty to do so. The dirty html comment hack will have to do for now.

1 Like

A post was split to a new topic: Help with define blocks