router

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package router maps URL paths to screens. With folder routing, a project's pages/ directory is the map: each folder holding a page.go is a route, and a folder named _name is a parameter.

pages/page.go               /
pages/orders/page.go        /orders
pages/orders/_id/page.go    /orders/:id

`nimbus native gen` (run by every build) turns the folders into a static table, pages/routes_gen.go, so matching costs a few string compares and no reflection. Each page.go declares a Page component; parameters land in fields tagged param:

package order // pages/orders/_id

type Page struct {
	ID int `param:"id"`
}

func (p Page) Title() string { return fmt.Sprintf("Order #%d", p.ID) } // optional
func (p Page) Render(c *ui.Ctx) *ui.Element { ... }

Register the table once, then navigate by path from any screen:

router.Register(pages.Routes)
router.Push(c, "/orders/42")

Index

Constants

This section is empty.

Variables

View Source
var NavigatorKey any = navigatorKey{}

NavigatorKey is the ui.SessionValue key of a session's Navigator.

Functions

func Link(path string, s ui.Style, children ...*ui.Element) *ui.Element

Link is a pressable that goes to path: router.Push when tapped, and on the web a real link that crawlers and new tabs follow.

router.Link("/orders/42", ui.Style{}, ui.Text("Order #42", ui.Style{}))

func Patterns

func Patterns() []string

Patterns lists the routes' patterns, most specific first: the web host builds its sitemap from the ones without parameters.

func Push

func Push(c *ui.Ctx, path string) bool

Push shows the page at path on top of the Stack the component is in, titled by its Title method or else its last static path segment. Outside a Stack, on the web, it goes to that page and its URL. It reports false, and changes nothing, when no route matches.

func Register

func Register(t Table)

Register installs the app's routes, replacing any before. Call it from init, next to ui.Mount.

func TitleOf

func TitleOf(comp ui.Component, path string) string

TitleOf is a page's title: its Title method, else its path's last static segment, capitalized ("/orders/42" → "Orders").

Types

type Head struct {
	Title       string
	Description string
	Image       string // absolute URL of a preview image (og:image)
	NoIndex     bool   // keep the page out of search results
}

Head is what a page tells search engines and link previews: the web host puts it in the page's <head>. Pages provide it with a method:

func (p Page) Head() router.Head {
	return router.Head{Title: "Order #42", Description: "…"}
}

func HeadOf

func HeadOf(comp ui.Component, path string) Head

HeadOf is a page's Head: its Head method, else its title.

type Navigator interface{ Navigate(path string) bool }

Navigator moves between pages where there is no Stack: the web host implements it to change the page and the browser's URL. A session puts it in its values under NavigatorKey.

type Params

type Params map[string]string

Params are a matched path's parameters by name.

func Match

func Match(path string) (ui.Component, Params, bool)

Match finds the page for a path ("/orders/42?tab=items"): its component and parameters, with query values added. false when no route matches.

func (Params) Get

func (p Params) Get(name string) string

Get is a parameter's value ("" if absent).

func (Params) Int

func (p Params) Int(name string) (int, bool)

Int is a parameter as an int; false when it isn't one.

func (Params) Int64

func (p Params) Int64(name string) (int64, bool)

Int64 is a parameter as an int64; false when it isn't one.

type Route

type Route struct {
	Pattern string
	New     func(Params) (ui.Component, bool)
}

Route is one page: its pattern ("/orders/:id") and how to make its component from the parameters, false when they don't parse.

type Table

type Table []Route

Table is an app's routes.

type Titled

type Titled interface{ Title() string }

Titled is a page that names its screen; Push uses it for the bar title.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL