Define block super like Django

Hello,

I would like to utilize the parent contents of a block in a child template.

Use case: _default/baseof.html

<body class="{{- block "body_classes" . }}o-container{{- end }}">

Then, in child template index.html:

{{ define "body_classes" }}
    SUPER HERE????
    u-background-light3
{{ end }}

Django exposes a variable in the child block:

If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (see the next section), since it was already escaped, if necessary, in the parent template.

Is there anything similar in Hugo?

I could do this:

<body class="o-container {{- block "body_classes" . }}{{- end }}">

But I like having the option of overriding the whole thing, or being able to append.

This can also be helpful technique for meta tags, like the <title> if you want to keep existing copy, and put more copy before or after.

I am probably overlooking something in the manual?

You could include conditionals in _baseof.html which can set the body class based on the page type:

{{ $bodyClass := "default" }}
{{ if eq .Type "mySectionName" }} {{ $bodyClass = "overridden" }}
<body class={{ $bodyClass }}>
…

It may not be ideal if you are going to override lots of different section types. If you are going to be overriding it a lot, If would say move your <body> tag into the child template altogether, defining it inside of main.

1 Like

Thank you for the help! I really appreciate it. :slight_smile: