diff --git a/modules/context/context.go b/modules/context/context.go
index b75ba9ab67b00..98c1a9bdb6705 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -141,6 +141,7 @@ func Contexter() func(next http.Handler) http.Handler {
// TODO: "install.go" also shares the same logic, which should be refactored to a general function
ctx.TemplateContext = NewTemplateContext(ctx)
ctx.TemplateContext["Locale"] = ctx.Locale
+ ctx.TemplateContext["AvatarUtils"] = templates.NewAvatarUtils(ctx)
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index d9c297411a4db..8fedc4076a4d1 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -52,14 +52,11 @@ func NewFuncMap() template.FuncMap {
// -----------------------------------------------------------------
// svg / avatar / icon
- "svg": svg.RenderHTML,
- "avatar": Avatar,
- "avatarHTML": AvatarHTML,
- "avatarByAction": AvatarByAction,
- "avatarByEmail": AvatarByEmail,
- "EntryIcon": base.EntryIcon,
- "MigrationIcon": MigrationIcon,
- "ActionIcon": ActionIcon,
+ "svg": svg.RenderHTML,
+ "avatarHTML": AvatarHTML,
+ "EntryIcon": base.EntryIcon,
+ "MigrationIcon": MigrationIcon,
+ "ActionIcon": ActionIcon,
"SortArrow": SortArrow,
diff --git a/modules/templates/util_avatar.go b/modules/templates/util_avatar.go
index 81961041a0ca9..462668588a316 100644
--- a/modules/templates/util_avatar.go
+++ b/modules/templates/util_avatar.go
@@ -18,6 +18,14 @@ import (
"code.gitea.io/gitea/modules/setting"
)
+type AvatarUtils struct {
+ ctx context.Context
+}
+
+func NewAvatarUtils(ctx context.Context) *AvatarUtils {
+ return &AvatarUtils{ctx: ctx}
+}
+
// AvatarHTML creates the HTML for an avatar
func AvatarHTML(src string, size int, class, name string) template.HTML {
sizeStr := fmt.Sprintf(`%d`, size)
@@ -30,44 +38,44 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
}
// Avatar renders user avatars. args: user, size (int), class (string)
-func Avatar(ctx context.Context, item any, others ...any) template.HTML {
+func (au *AvatarUtils) Avatar(item any, others ...any) template.HTML {
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
switch t := item.(type) {
case *user_model.User:
- src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
+ src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
if src != "" {
return AvatarHTML(src, size, class, t.DisplayName())
}
case *repo_model.Collaborator:
- src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
+ src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
if src != "" {
return AvatarHTML(src, size, class, t.DisplayName())
}
case *organization.Organization:
- src := t.AsUser().AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
+ src := t.AsUser().AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
if src != "" {
return AvatarHTML(src, size, class, t.AsUser().DisplayName())
}
}
- return template.HTML("")
+ return ""
}
// AvatarByAction renders user avatars from action. args: action, size (int), class (string)
-func AvatarByAction(ctx context.Context, action *activities_model.Action, others ...any) template.HTML {
- action.LoadActUser(ctx)
- return Avatar(ctx, action.ActUser, others...)
+func (au *AvatarUtils) AvatarByAction(action *activities_model.Action, others ...any) template.HTML {
+ action.LoadActUser(au.ctx)
+ return au.Avatar(action.ActUser, others...)
}
// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
-func AvatarByEmail(ctx context.Context, email, name string, others ...any) template.HTML {
+func (au *AvatarUtils) AvatarByEmail(email, name string, others ...any) template.HTML {
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
- src := avatars.GenerateEmailAvatarFastLink(ctx, email, size*setting.Avatar.RenderedSizeFactor)
+ src := avatars.GenerateEmailAvatarFastLink(au.ctx, email, size*setting.Avatar.RenderedSizeFactor)
if src != "" {
return AvatarHTML(src, size, class, name)
}
- return template.HTML("")
+ return ""
}
diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go
index 0e232c194c2e1..b1cb42297c1a6 100644
--- a/routers/web/repo/blame.go
+++ b/routers/web/repo/blame.go
@@ -235,6 +235,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
var lexerName string
+ avatarUtils := templates.NewAvatarUtils(ctx)
i := 0
commitCnt := 0
for _, part := range blameParts {
@@ -257,9 +258,9 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
var avatar string
if commit.User != nil {
- avatar = string(templates.Avatar(ctx, commit.User, 18, "gt-mr-3"))
+ avatar = string(avatarUtils.Avatar(commit.User, 18, "gt-mr-3"))
} else {
- avatar = string(templates.AvatarByEmail(ctx, commit.Author.Email, commit.Author.Name, 18, "gt-mr-3"))
+ avatar = string(avatarUtils.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "gt-mr-3"))
}
br.Avatar = gotemplate.HTML(avatar)
diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl
index 35aff8e538251..1ad7fd20bd933 100644
--- a/templates/base/head_navbar.tmpl
+++ b/templates/base/head_navbar.tmpl
@@ -57,7 +57,7 @@
{{if and .IsSigned .MustChangePassword}}
- {{avatar $.Context .SignedUser 24 "gt-mr-2"}}
+ {{ctx.AvatarUtils.Avatar .SignedUser 24 "gt-mr-2"}}
{{.SignedUser.Name}}
{{svg "octicon-triangle-down"}}
@@ -143,7 +143,7 @@
- {{avatar $.Context .SignedUser 24 "gt-mr-2"}}
+ {{ctx.AvatarUtils.Avatar .SignedUser 24 "gt-mr-2"}}
{{.SignedUser.Name}}
{{svg "octicon-triangle-down"}}
diff --git a/templates/explore/users.tmpl b/templates/explore/users.tmpl
index 5e150f692f3ca..1280f4add6641 100644
--- a/templates/explore/users.tmpl
+++ b/templates/explore/users.tmpl
@@ -8,7 +8,7 @@
{{range .Users}}
- {{avatar $.Context . 48}}
+ {{ctx.AvatarUtils.Avatar . 48}}
diff --git a/templates/org/header.tmpl b/templates/org/header.tmpl
index 6106fe5d2b2ea..9348e14544fed 100644
--- a/templates/org/header.tmpl
+++ b/templates/org/header.tmpl
@@ -3,7 +3,7 @@
{{- if .CanCommitToBranch.WillSign}} {{svg "octicon-lock" 24}} diff --git a/templates/repo/forks.tmpl b/templates/repo/forks.tmpl index 13810b44da8f9..481c0e67e099c 100644 --- a/templates/repo/forks.tmpl +++ b/templates/repo/forks.tmpl @@ -7,7 +7,7 @@ {{range .Forks}}
- {{avatar $.Context .Owner}}
+ {{ctx.AvatarUtils.Avatar .Owner}}
{{.Owner.Name}} / {{.Name}}
{{end}}
diff --git a/templates/repo/graph/commits.tmpl b/templates/repo/graph/commits.tmpl
index 8652a720522d3..0a21aec9c0826 100644
--- a/templates/repo/graph/commits.tmpl
+++ b/templates/repo/graph/commits.tmpl
@@ -64,10 +64,10 @@
{{if $commit.User.FullName}}
{{$userName = $commit.User.FullName}}
{{end}}
- {{avatar $.Context $commit.User}}
+ {{ctx.AvatarUtils.Avatar $commit.User}}
{{$userName}}
{{else}}
- {{avatarByEmail $.Context $commit.Commit.Author.Email $userName}}
+ {{ctx.AvatarUtils.AvatarByEmail $commit.Commit.Author.Email $userName}}
{{$userName}}
{{end}}
diff --git a/templates/repo/issue/filters.tmpl b/templates/repo/issue/filters.tmpl
index b75d98ed6f39d..799509328fe3f 100644
--- a/templates/repo/issue/filters.tmpl
+++ b/templates/repo/issue/filters.tmpl
@@ -146,7 +146,7 @@
{{range .Assignees}}
- {{avatar $.Context . 20}}{{template "repo/search_name" .}}
+ {{ctx.AvatarUtils.Avatar . 20}}{{template "repo/search_name" .}}
{{end}}