chore: move template context (#8663)

The template module now holds the **Template** context, this makes it possible for (render) function in the template module to access functions and share data between render functions.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8663
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Lucas <sclu1034@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-07-25 11:55:15 +02:00 committed by Earl Warren
commit d4e4a2a1e3
16 changed files with 88 additions and 70 deletions

View file

@ -41,7 +41,7 @@ type Render interface {
type Context struct {
*Base
TemplateContext TemplateContext
TemplateContext *templates.Context
Render Render
PageData map[string]any // data used by JavaScript modules in one page, it's `window.config.pageData`
@ -64,8 +64,6 @@ type Context struct {
Package *Package
}
type TemplateContext map[string]any
func init() {
web.RegisterResponseStatusProvider[*Context](func(req *http.Request) web_types.ResponseStatusProvider {
return req.Context().Value(WebContextKey).(*Context)
@ -98,10 +96,11 @@ func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
return ctx
}
func NewTemplateContextForWeb(ctx *Context) TemplateContext {
tmplCtx := NewTemplateContext(ctx)
tmplCtx["Locale"] = ctx.Locale
tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx)
func NewTemplateContextForWeb(ctx *Context) *templates.Context {
tmplCtx := templates.NewContext(ctx)
tmplCtx.Locale = ctx.Locale
tmplCtx.AvatarUtils = templates.NewAvatarUtils(ctx)
tmplCtx.Data = ctx.Data
return tmplCtx
}