Skip to main content
In this guide, you’ll create a simple web page with a layout and dynamic content. By the end, you’ll understand the core concepts of Mizu’s view system.

Prerequisites

  • Go 1.22 or later installed
  • Basic understanding of Go and HTML
  • A text editor

Step 1: Create Your Project

First, create a new directory and initialize a Go module:

Step 2: Create the Directory Structure

The view package expects templates in a specific structure:
Your project should look like this:

Step 3: Create the Layout

A layout is the HTML shell that wraps your pages. Create views/layouts/default.html:
What’s happening here?
  • {{.Data.Title}} - Accesses the “Title” field from data you pass
  • {{.Content}} - This is where the page content gets inserted
  • The layout provides the common structure (doctype, head, nav, footer)

Step 4: Create Pages

Pages are templates that provide the main content. Create views/pages/home.html:
What’s happening here?
  • {{.Data.Name}} - Accesses data passed from your handler
  • {{range .Data.Features}}...{{end}} - Loops through a list
  • {{.}} - Inside range, this is the current item
  • Page content is automatically inserted into the layout’s {{.Content}}
Create views/pages/about.html:

Step 5: Create the Go Application

Now create main.go:
What’s happening here?
  1. Create engine - view.New() creates a view engine with your configuration
  2. Add middleware - app.Use(engine.Middleware()) stores the engine in every request context
  3. Render pages - view.Render(c, "home", data) renders the “home” page with data

Step 6: Run Your Application

Start the server:
Now open your browser to http://localhost:8080. You should see:
  • The home page with your name and features list
  • A styled button
  • Navigation to the About page
Try editing views/pages/home.html and refreshing your browser - the changes appear instantly because Development mode is enabled!

Understanding the Data Flow

Here’s what happens when someone visits your site:

Understanding Template Data

Templates receive a wrapper structure, not just your data directly:

Common Patterns

Using a Different Layout

Override the default layout for specific pages:
This uses views/layouts/admin.html instead of default.html.

Rendering Without a Layout

For partial HTML responses (AJAX, htmx, etc.):

Using the Engine Directly

If you need the engine instance:

Next Steps

You’ve learned the basics! Here’s what to explore next:
  • Engine - All configuration options explained
  • Templates - Deep dive into Go template syntax
  • Layouts - Advanced layout patterns
  • Functions - Built-in and custom template functions
  • Production - Embedding templates and caching for production

Troubleshooting

”template not found” Error

Check that:
  1. Your views directory exists with layouts/ and pages/ subdirectories
  2. The file extension matches your Config (default is .html)
  3. The name in Render() matches the filename in pages/ (without extension)

Changes Not Appearing

Make sure Development: true is set in your view config. In production mode (the default), templates are cached.

”layout not found” Error

Make sure the layout file exists at views/layouts/default.html (or your custom DefaultLayout value).