feat: add ctx.IsRoute for active-nav highlighting - #122
Merged
Conversation
The final Tier-2 website-DX item. ctx.IsRoute(name) reports whether the
named route is the most specific (deepest) registered route matching the
current request path, so the active nav link is highlighted without its
parent sections also lighting up: on /admin/users/42, IsRoute("users") is
true but IsRoute("admin") is false. Matching is path-segment aware
("/admin" matches "/admin/x" but not "/administrators"), so "/" is active
only on exactly "/"; any query string or trailing slash in the route path
is normalized away.
It is a Context method (not a bare template func), so templates use it as
{{.IsRoute "home"}} by passing ctx as the view data. This avoids
per-request template-func binding (and its template-clone cost and
thread-safety concerns) entirely: html/template already calls methods on
the data, and the data flows into components too.
Panics on an unknown route, like Route. stdlib only, 100% covered.
Verified with go vet, go test -race, gofmt.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The final Tier-2 item.
ctx.IsRoute(name)reports whether the named route is the most specific (deepest) registered route matching the current request path — for highlighting the active nav link.Matching (best-match)
Routes
Home=/,Admin=/admin,Users=/admin/users; on/admin/users/42:IsRoute(...)"users""admin""home"/adminmatches/admin/xbut not/administrators./is active only on exactly/./admin) stays active on its sub-paths (/admin/settings) when no deeper route matches./docs/behaves like/docs), via a smallroutePathhelper.Why a method, not a
{{isRoute}}template funcA bare
{{isRoute}}func can't see the request: hime's template funcs bind at parse time on the sharedapp.parent, and rebinding per request would force a template clone on every render (required for thread-safety) plus awkward nested-{{component}}handling.html/templatealready calls methods on the data, so aContextmethod used as{{.IsRoute "x"}}(passctxas the data) gives request-aware highlighting with zero clone, zero core-render change, thread-safe, and it works inside components too. Bonus: it makes.IsHTMX,.CookieValue, … usable from templates the same way.Note
Best-match scans the route table per call (O(routes)) and couples a route's result to the whole table (registering a deeper route can change a shallower one's highlight) — that coupling is the intended "most specific wins" behavior. Negligible cost for realistic route tables.
Test plan
go vet ./...go test ./...— 100% coverage onIsRoute,routePath,pathUnder; covers exact / sub-path / section / segment-aware / root / query / trailing-slash / panic cases, plus an in-template test (View("nav", ctx)→{{.IsRoute}})go test -race ./...gofmtcleanExampleContext_IsRoute; CLAUDE.md updatedCompletes Tier 2: signed cookies → flash → FormState → IsRoute.
🤖 Generated with Claude Code