Repository navigation
Route
Route is a map[string]string used for global route storage
like Global.
It's handy to use Route to store routes combined with ctx.RedirectTo
app.Routes(hime.Routes{
"home": "/",
"about-us": "/about-us",
"user": "/user",
"product": "/product",
})Route can retrieve from app.Route, ctx.Route, and template func route
<a href="{{route "about-us"}}">About Us</a>
For redirect, using RedirectTo will redirect to named route.
func notFound(ctx *hime.Context) error {
return ctx.RedirectTo("home")
}Path param can be added to route.
{{range .List}}
<a href="{{route "user" .ID}}">{{.Name}}</a>
{{end}}
Output
<a href="/user/3">Hime</a>Or using ctx.Route, app.Route
ctx.Route("user", user.ID)
ctx.RedirectTo("user", user.ID)Use hime.Param, ctx.Param or template func param.
ctx.Route("product", ctx.Param("id", 6)) // /product?id=6
ctx.RedirectTo("product", ctx.Param("name", "hime")) // /product?name=hime<a href="{{route "product" "ebook" (param "name" "hime") (param "id" 2)}}">Hime</a>
Output
<a href="/product/ebook?name=hime&id=2">Hime</a>ctx.IsRoute(name) reports whether a route is the most specific match for the
current request path — handy for highlighting the active navigation link. Pass
ctx as the view data so the template can call it.
func page(ctx *hime.Context) error {
return ctx.View("page", ctx)
}<a href="{{route "users"}}" class="{{if .IsRoute "users"}}active{{end}}">Users</a>On /admin/users/42 the /admin/users route is active but its /admin parent
is not (most specific wins). Matching is path-segment aware, so / is active
only on exactly /.