11.8 Comments in Templates

Right, comments. The part of the template where you talk to yourself, to future you, or to the poor soul who has to maintain your code after you’ve been promoted away from this mess. In a language where logic and presentation are smashed together, comments aren’t just nice-to-have; they’re a survival tool. Let’s be honest, the Go template syntax can get… gnarly. A well-placed comment is a beacon of clarity in a storm of curly braces and dot notations.

11.7 define and block: Named Templates

Now, let’s get into the good stuff: making your templates modular and reusable. You’ve met {{ define }} and {{ template }}, which are the basic building blocks. But what happens when you want to define a default version of a block that can be optionally overridden by a more specific template? You can’t just {{ define }} the same thing twice; that’s a one-way ticket to a template compilation error. This is where {{ block }} comes in, and it’s Hugo’s secret weapon for creating flexible, extensible layouts without the copy-paste madness.

11.6 with: Scoping to a Non-Nil Value

Right, let’s talk about with. This is one of those template actions that seems simple until you stare into its abyss and the abyss stares back. It’s not just a fancy if statement. It’s a scope-changer. Think of it as a temporary, focused lens you hold up to a specific piece of your data. Its primary job is to say, “For everything inside this block, stop looking way over there. The thing you need is right here.”

11.5 Iteration: range and Its Variables

Right, let’s talk about iteration. This is where Go templates stop being a fancy variable replacer and start feeling like a real programming language. The workhorse here is the range action, and it’s your best friend for looping through all your content, tags, or any slice of data Hugo hands you. The basic syntax is simple enough. You’ve got your collection, you range over it, and you do something with each item.

11.4 Conditionals: if, else, else if

Right, conditionals. This is where your templates stop being dumb document stampers and start making actual decisions. It’s the “if this, then that” logic that makes your site dynamic. Go templates give you if, else if, and else. The syntax is blessedly straightforward, but the devil, as always, is in the details. Let’s start with the basic structure. It looks a lot like most programming languages, just with those mustache-like braces.

11.3 Variables: $ and . (dot)

Right, let’s talk about the two things you’ll be throwing around more than confetti at a Go template party: the dollar sign $ and the dot .. At first glance, they seem like simple, almost trivial concepts. And then you try to use them, and you feel like you’re trying to explain quantum mechanics to a golden retriever. Don’t worry, we’ve all been there. It’s not you; it’s the syntax. Let’s demystify it.

11.2 Whitespace Control: - in Actions

Right, let’s talk about whitespace. It’s the silent, invisible monster that will absolutely ruin your beautifully crafted HTML output, leaving you with a minified-looking mess or, worse, a single line of text that breaks your entire layout. Go’s template engine is notoriously, almost comically, enthusiastic about printing every single character you tell it to, including all the newlines and tabs you put in your template for readability. This is where the magic dash (-) inside your actions comes in. It’s not just a suggestion; it’s your primary weapon in the war for clean output.

11.1 Template Syntax: Actions, Pipelines, and Variables

Alright, let’s get our hands dirty with the syntax. This is where we move from staring at the blueprint to actually swinging the hammer. Go’s template syntax is elegant in its minimalism, but that minimalism can be deceptive. It’s a small set of tools, but you have to learn the precise, slightly opinionated way the designers intended you to use them. Master these fundamentals, and the rest of Hugo falls neatly into place.

— joke —

...