Skip to content

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.

func NavItems(ctx context.Context) []NavItem

Returns navigation items injected by the framework middleware. Returns nil if no items are set.

WithNavItems

func WithNavItems(ctx context.Context, items []NavItem) context.Context

Stores navigation items in the context. Used internally by the framework.

WithAuthChecker

func WithAuthChecker(ctx context.Context, checker AuthChecker) context.Context

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.

type AuthChecker struct {
    IsAuthenticated func() bool
    IsAdmin         func() bool
}

Only needed when using a custom auth system instead of the auth contrib app.

Layout

func Layout(ctx context.Context) string

Returns the app layout template name from the context. Returns "" if no layout is set.

WithLayout

func WithLayout(ctx context.Context, name string) context.Context

Stores the app layout template name in the context. Used internally by the framework middleware.

RequestPath

func RequestPath(ctx context.Context) string

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

func WithRequestPath(ctx context.Context, path string) context.Context

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

func WithTemplateExecutor(ctx context.Context, exec TemplateExecutor) context.Context

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.

func NavGroups(ctx context.Context) []NavGroup

Returns the admin nav groups from the context. Each NavGroup contains an app name and its navigation items. Returns nil if not set.

Deprecated: NavGroupsFromContext will be removed in a future version. Use NavGroups instead.

WithNavGroups

func WithNavGroups(ctx context.Context, groups []NavGroup) context.Context

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

func Token(ctx context.Context) string

Returns the CSRF token from the context. Returns "" if not set.

WithToken

func WithToken(ctx context.Context, token string) context.Context

Stores a CSRF token in the context. Used internally by the CSRF middleware.

Auth Helpers

Defined in github.com/oliverandrich/burrow/contrib/auth.

CurrentUser

func CurrentUser(ctx context.Context) *User

Returns the authenticated user from the context, or nil if not logged in.

Deprecated: UserFromContext will be removed in a future version. Use CurrentUser instead.

IsAuthenticated

func IsAuthenticated(ctx context.Context) bool

Returns true if a user is logged in.

WithUser

func WithUser(ctx context.Context, user *User) context.Context

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

func Inject(r *http.Request, values map[string]any) *http.Request

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

func Locale(ctx context.Context) string

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:

ve.Translate(ctx, i18n.TData)

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

func URL(ctx context.Context, name string) string

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

func Broker(ctx context.Context) *EventBroker

Returns the *EventBroker from the context. Injected by the SSE middleware.

Messages Helpers

Defined in github.com/oliverandrich/burrow/contrib/messages.

Get

func Get(ctx context.Context) []Message

Returns flash messages from the context. Returns nil if no messages are set.

Add

func Add(w http.ResponseWriter, r *http.Request, level, text string)

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

func RetryAfter(ctx context.Context) time.Duration

Returns the duration until the client can retry. Set by the rate limit middleware when a request is throttled.