Skip to content

Hugo Build Error Undefined

DodaTech 2 min read

In this tutorial, you'll learn about Hugo Build Error: Undefined Variable Fix. We cover key concepts, practical examples, and best practices.

The Problem

You run hugo and get:

ERROR: render of "page" failed: ".../content/post.md:1:1": execute of template
failed: template: _default/single.html:10:5: executing "main" at <$title>:
can't evaluate field title in type page.Page

Hugo templates throw errors when referencing variables that do not exist in the current context.

Wrong

<h1>{{ $title }}</h1>

Output: ERROR: can't evaluate field title in type page.Page

$title is not automatically available. You must access page variables through the . context.

<h1>{{ .Title }}</h1>

Output: Hello World (the page title)

Hugo passes the page object as the dot (.) in templates. Use .Title, .Content, .Date etc.

Prevention

  • Always prefix page variables with a dot, e.g. .Title
  • Use {{ with .Param "key" }}{{ . }}{{ end }} for optional frontmatter fields
  • Check Hugo's template variable scope documentation

Common Mistakes with build error undefined

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world HUGO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does $title show an error in Hugo?

$title is a template-scoped variable that must be declared first with :=. Page-level variables use .Title — the dot refers to the current context (the page object).

How do I check if a variable exists in Hugo?

Use {{ if .Title }}{{ .Title }}{{ end }} or {{ with .Title }}{{ . }}{{ end }} to safely check and render a variable without errors.

What is the dot context in Hugo templates?

The dot (.) represents the current context — the page object in page templates, the current menu entry in menu templates, or the current slice element in range blocks. Always consider what . refers to in each scope.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro