Context Helpers¶
The framework and contrib apps store values in the request context. These helpers read and write those values.
Core Helpers¶
Defined in github.com/oliverandrich/burrow.
NavItems¶
Returns navigation items injected by the framework middleware. Returns nil if no items are set.
WithNavItems¶
Stores navigation items in the context. Used internally by the framework.
WithAuthChecker¶
Stores an AuthChecker in the context. The auth contrib app calls this automatically in its middleware. The core navLinks template function reads it to filter AuthOnly/AdminOnly nav items. When no AuthChecker is set, those items are hidden.
Only needed when using a custom auth system instead of the auth contrib app.
Layout¶
Returns the app layout template name from the context. Returns "" if no layout is set.
WithLayout¶
Stores the app layout template name in the context. Used internally by the framework middleware.
RequestPath¶
Returns the current request path from the context. Set automatically by the template middleware for HTTP requests. Apps can use this to compute active-link state in custom navigation templates. Returns "" if not set.
WithRequestPath¶
Stores the request path in the context. Used internally for HTTP requests; non-HTTP renderers (background jobs, SSE, CLI) should set it explicitly when nav-link highlighting is needed.
TemplateExec¶
func TemplateExec(ctx context.Context) TemplateExecutor
type TemplateExecutor func(ctx context.Context, name string, data map[string]any) (template.HTML, error)
Returns the template executor injected by the framework. For non-HTTP rendering, obtain one via Server.TemplateExecutor() after boot and store it with WithTemplateExecutor. Used internally by Render and RenderFragment.
WithTemplateExecutor¶
Stores a template executor in the context.
Generic Helpers¶
func WithContextValue(ctx context.Context, key, val any) context.Context
func ContextValue[T any](ctx context.Context, key any) (T, bool)
Generic context value helpers. ContextValue is a typed getter that returns the value and a boolean indicating whether it was found.
Admin Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/admin.
NavGroups¶
Returns the admin nav groups from the context. Each NavGroup contains an app name and its navigation items. Returns nil if not set.
Deprecated:
NavGroupsFromContextwill be removed in a future version. UseNavGroupsinstead.
WithNavGroups¶
Stores admin nav groups in the context. Used internally by the admin route middleware.
Note
The admin layout template name is injected via burrow.WithLayout(ctx, "admin/layout") inside the /admin route group — there is no separate admin.Layout or admin.WithLayout helper. The request path lives in the root burrow package — use burrow.RequestPath(ctx) (see Core Helpers). admin.RequestPath / admin.WithRequestPath / admin.RequestPathFromContext still exist as deprecated thin wrappers and will be removed in a future release.
CSRF Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/csrf.
Token¶
Returns the CSRF token from the context. Returns "" if not set.
WithToken¶
Stores a CSRF token in the context. Used internally by the CSRF middleware.
Auth Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/auth.
CurrentUser¶
Returns the authenticated user from the context, or nil if not logged in.
Deprecated:
UserFromContextwill be removed in a future version. UseCurrentUserinstead.
IsAuthenticated¶
Returns true if a user is logged in.
WithUser¶
Stores the user in the context. Used internally by the auth middleware.
Session Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/session.
Getters¶
func GetString(r *http.Request, key string) string
func GetInt64(r *http.Request, key string) int64
func GetValues(r *http.Request) map[string]any
Read values from the session. Return zero values if the key is missing.
Setters¶
func Set(w http.ResponseWriter, r *http.Request, key string, value any) error
func Delete(w http.ResponseWriter, r *http.Request, key string) error
func Save(w http.ResponseWriter, r *http.Request, values map[string]any) error
func Clear(w http.ResponseWriter, r *http.Request)
Write values to the session. Set, Delete, and Save immediately write the cookie and return an error if the session middleware is not active.
Testing¶
Sets up session state in the request context without the full middleware. Intended for use in tests. Returns a new *http.Request with the injected values.
i18n Helpers¶
Defined in github.com/oliverandrich/burrow/i18n.
Translation¶
func T(ctx context.Context, key string) string
func TData(ctx context.Context, key string, data map[string]any) string
func TPlural(ctx context.Context, key string, count int) string
Translate messages using the localizer from context. Fall back to the message ID if no translation is found.
Locale¶
Returns the current locale (e.g., "en", "de"). Defaults to "en".
Translating Validation Errors¶
Validation errors are translated via the Translate method on *burrow.ValidationError, not a standalone function:
For each FieldError, looks up the key "validation-" + tag (e.g., "validation-required") with {"Field": fe.Field, "Param": fe.Param} as template data. If a translation is found, replaces Message; otherwise preserves the original English message.
Static Files Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/staticfiles.
URL¶
Returns the content-hashed URL for a static file. Falls back to the original name if the file is not in the manifest or the middleware is not active.
SSE Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/sse.
Broker¶
Returns the *EventBroker from the context. Injected by the SSE middleware.
Messages Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/messages.
Get¶
Returns flash messages from the context. Returns nil if no messages are set.
Add¶
Adds a flash message to the session. The message will be available on the next request.
Rate Limit Helpers¶
Defined in github.com/oliverandrich/burrow/contrib/ratelimit.
RetryAfter¶
Returns the duration until the client can retry. Set by the rate limit middleware when a request is throttled.