Example: Blog Post by Slug

See a working code example that demonstrates dynamic route logic with a slug.

Example: Blog Post by Slug

// routes/blog/_slug/index.server.go
package main

import (
	"net/http"
)

func HandleRequest(r *http.Request, params map[string]string) (map[string]interface{}, error) {
	slug := params["slug"]

	post := map[string]interface{}{
		"title":   "Hello from " + slug,
		"content": "This content is dynamically generated for " + slug,
	}

	return post, nil
}

This is a classic use case: dynamically injecting data based on a slug. The returned post values will be available in your template under {{ .title }} and {{ .content }}.

To learn how this integrates with templates, visit the Template Data section.