Some new Hugo learnings
I’m once again trying to port my ancient Blosxom blog to a static engine. Focusing on Hugo this time. I no longer care which engine I use, just want to use one, and the most popular one is probably a good choice. I am adamant that I have to write my own theme, collection of templates and layouts and stuff. I want complete control over site layout and page contents.
Most tutorials are toys that never talk about pagination. Many have a default home page of “all your posts”. This seems to be for crummy marketing sites with 3 pieces of content and are not useful for a real blog with 1000+ posts. Pagination is a whole complex topic I don’t understand yet.
A lightbulb went off when I looked at this classless-blog Hugo theme. It has no Javascript. It produces really simple semantic HTML with no CSS classes, just clean tags like <header> or <main>. You can then change how the page looks with pure CSS, perhaps starting with one of these classless CSS resets. That is the One True Way to web development, IMHO, at least for simple static textual content. I’m glad to see a clear path to doing it in Hugo. (Seriously, people are stuffing React into Hugo blogs? Ugh.) Unfortunately the classless-blog theme isn’t really suitable for me to just use (it doesn’t have the right structure for me) but I can steal a lot of ideas from it.
As for writing my own theme, I’m finding this tutorial really helpful. It’s from 2020, so a little out of date, but mostly good.
The tutorial neglects to mention at the start that you have to set the theme= in your configuration TOML. That file recently got renamed from config.toml to hugo.toml. Fun fact; if you don’t set that variable, Hugo throws an opaque error message.
WARN found no layout file for “html” for kind “home”: You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
What that error really means is “theme not found so you have no layouts at all for your blog”. It’d sure be nice if the tool caught this simple beginner’s error.
Hugo themes are programmed in Go template languages. Last time I encountered this I got mad but I read the excellent Go docs and now understand it’s pretty simple with just a few control flow items. The weird thing is some syntax like {{if}} are genuine control flow things, others like {{printf}} are more like function calls. Also there’s a {{define}} which creates new functions. The confusing part about Hugo templates is the implicit context hidden in . the cursor and how {{range}} is a looping construct. Also it’s still not clear how often templates are invoked. But...
Hugo has a concept of “Kinds”. These are the various sorts of pages that Hugo will create as it runs and it’s a little confusing and poorly documented. The docs here for .Kind are the most explicit I could find.
The page’s kind. Possible return values are page, home, section, taxonomy, or term. Note that there are also RSS, sitemap, robotsTXT, and 404 kinds, but these are only available during the rendering of each of these respective page’s kind and therefore not available in any of the Pages collections.
Each of these kinds is in turn instantiated by a template file. Hugo will look in many places for the template for a kind, with up to 46(!) different paths. The template lookup order docs are essential for understanding this.
It’d be nice if Hugo had a debug mode which printed every HTML page it created, why (what kind maybe?), and every template that was used to create that. That’d really help understanding what the generator is actually doing. I did find hugo --printUnusedTemplates useful for at least finding when template files weren’t being used.
Comments
Why not write your own in atom or rss?