Documentation menu

Pages

Pages are the entry point of an htmgo application.

A simple page may look like:

Copy
 1package pages
 2
 3import "github.com/franchb/htmgo/framework/v2/h"
 4
 5func HelloHtmgoPage(ctx *h.RequestContext) *h.Page {
 6	return h.NewPage(
 7		h.Html(
 8			h.Head(
 9				h.Link("/public/main.css", "stylesheet"),
10				h.Script("/public/htmgo.js"),
11			),
12			h.Body(
13				h.P(h.Text("Hello, htmgo!")),
14			),
15		),
16	)
17}

A page handler accepts *h.RequestContext, which wraps Fiber v3's fiber.Ctx, and returns *h.Page. Put this one in pages/hello.go to serve it at /hello.

Automatic Routing

htmgo registers GET routes with Fiber for exported functions under pages/ with a single named *h.RequestContext parameter and a *h.Page return value. The file path determines the URL.

  • pages/index.go → /
  • pages/users.go → /users
  • pages/users.$id.go → /users/:id
Read the id route parameter with ctx.UrlParam("id").

Shared page layout

Use a shared function to reuse your page layout, stylesheets, and scripts. This helper belongs in pages/root.go in the pages package.

Copy
 1func RootPage(children ...h.Ren) *h.Page {
 2	return h.NewPage(
 3		h.Html(
 4			h.Head(
 5				h.Meta("viewport", "width=device-width, initial-scale=1"),
 6				h.Link("/public/main.css", "stylesheet"),
 7				h.Script("/public/htmgo.js"),
 8			),
 9			h.Body(children...),
10		),
11	)
12}

Call it from pages/users.$id.go in the same package.

Copy
1func UserPage(ctx *h.RequestContext) *h.Page {
2	return RootPage(
3		h.Pf("User ID: %s", ctx.UrlParam("id")),
4	)
5}

The route generator skips this RootPage because its signature accepts children. To exclude a file explicitly, add it to htmgo.yml.

automatic_page_routing_ignore: ["root.go"]
Copy