Nested blocks in base templates

I’m a little puzzled that I haven’t thought about this, but base blocks can be nested, which I think can be very useful to avoid repetition:

{{ block "rightsidebar" . }}
    <aside
      class="sticky top-[4.75rem] h-[calc(100vh-4.75rem)] w-92 bg-blue-100 p-4 overflow-y-auto">
      {{ block "rightsidebar_content" . }}{{ end }}
    </aside>
{{ end }}

And then in e.g. single.html:

{{ define "rightsidebar_content" }}
  Sidebar content.
{{ end }}

Or if you want no sidebar or a completely different one:

{{ define "rightsidebar" }}
  {{ printf "%c" '\u00A0' }}
{{ end }}

THe above is a trick; a template needs to render something non-space to be considered for a block replacement. The above renders a nonprintable Unicode character.

1 Like