I wanted to be able to setup custom layouts per post (or per page) on my Micro.blog site (which runs Hugo under the hood).

My hackish solution was to leverage the Categories feature to inject a body class into the page. From there, I can setup as many custom layouts as I want, based on the body tag.

If a Category is named “Layout-*” then whatever comes after “Layout-” will be injected as a body tag.

For example, “Layout-Aqua” becomes a body tag of “aqua”, which you can see in action here.

Here’s the code I’m using:

{{ $.Scratch.Set "layoutClasses" "" }} {{ range .Params.categories }} {{ $lowerCategory := lower . }} {{ if strings.HasPrefix $lowerCategory "layout-" }} {{ $.Scratch.Set "layoutClasses" (print ($.Scratch.Get "layoutClasses") " " (substr $lowerCategory 7 | urlize)) }} {{ end }} {{ end }}

body class="{{ $.Scratch.Get “layoutClasses” }}"