Skip to content

Commit a370efc

Browse files
authored
Use template context function for avatar rendering (#26385)
Introduce `AvatarUtils`, no need to pass `$.Context` to every sub-template, and simplify the template helper functions.
1 parent 36eb3c4 commit a370efc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+162
-155
lines changed

modules/context/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func Contexter() func(next http.Handler) http.Handler {
141141
// TODO: "install.go" also shares the same logic, which should be refactored to a general function
142142
ctx.TemplateContext = NewTemplateContext(ctx)
143143
ctx.TemplateContext["Locale"] = ctx.Locale
144+
ctx.TemplateContext["AvatarUtils"] = templates.NewAvatarUtils(ctx)
144145

145146
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
146147
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this

modules/templates/helper.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ func NewFuncMap() template.FuncMap {
5252

5353
// -----------------------------------------------------------------
5454
// svg / avatar / icon
55-
"svg": svg.RenderHTML,
56-
"avatar": Avatar,
57-
"avatarHTML": AvatarHTML,
58-
"avatarByAction": AvatarByAction,
59-
"avatarByEmail": AvatarByEmail,
60-
"EntryIcon": base.EntryIcon,
61-
"MigrationIcon": MigrationIcon,
62-
"ActionIcon": ActionIcon,
55+
"svg": svg.RenderHTML,
56+
"avatarHTML": AvatarHTML,
57+
"EntryIcon": base.EntryIcon,
58+
"MigrationIcon": MigrationIcon,
59+
"ActionIcon": ActionIcon,
6360

6461
"SortArrow": SortArrow,
6562

modules/templates/util_avatar.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import (
1818
"code.gitea.io/gitea/modules/setting"
1919
)
2020

21+
type AvatarUtils struct {
22+
ctx context.Context
23+
}
24+
25+
func NewAvatarUtils(ctx context.Context) *AvatarUtils {
26+
return &AvatarUtils{ctx: ctx}
27+
}
28+
2129
// AvatarHTML creates the HTML for an avatar
2230
func AvatarHTML(src string, size int, class, name string) template.HTML {
2331
sizeStr := fmt.Sprintf(`%d`, size)
@@ -30,44 +38,44 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
3038
}
3139

3240
// Avatar renders user avatars. args: user, size (int), class (string)
33-
func Avatar(ctx context.Context, item any, others ...any) template.HTML {
41+
func (au *AvatarUtils) Avatar(item any, others ...any) template.HTML {
3442
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
3543

3644
switch t := item.(type) {
3745
case *user_model.User:
38-
src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
46+
src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
3947
if src != "" {
4048
return AvatarHTML(src, size, class, t.DisplayName())
4149
}
4250
case *repo_model.Collaborator:
43-
src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
51+
src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
4452
if src != "" {
4553
return AvatarHTML(src, size, class, t.DisplayName())
4654
}
4755
case *organization.Organization:
48-
src := t.AsUser().AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
56+
src := t.AsUser().AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
4957
if src != "" {
5058
return AvatarHTML(src, size, class, t.AsUser().DisplayName())
5159
}
5260
}
5361

54-
return template.HTML("")
62+
return ""
5563
}
5664

5765
// AvatarByAction renders user avatars from action. args: action, size (int), class (string)
58-
func AvatarByAction(ctx context.Context, action *activities_model.Action, others ...any) template.HTML {
59-
action.LoadActUser(ctx)
60-
return Avatar(ctx, action.ActUser, others...)
66+
func (au *AvatarUtils) AvatarByAction(action *activities_model.Action, others ...any) template.HTML {
67+
action.LoadActUser(au.ctx)
68+
return au.Avatar(action.ActUser, others...)
6169
}
6270

6371
// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
64-
func AvatarByEmail(ctx context.Context, email, name string, others ...any) template.HTML {
72+
func (au *AvatarUtils) AvatarByEmail(email, name string, others ...any) template.HTML {
6573
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
66-
src := avatars.GenerateEmailAvatarFastLink(ctx, email, size*setting.Avatar.RenderedSizeFactor)
74+
src := avatars.GenerateEmailAvatarFastLink(au.ctx, email, size*setting.Avatar.RenderedSizeFactor)
6775

6876
if src != "" {
6977
return AvatarHTML(src, size, class, name)
7078
}
7179

72-
return template.HTML("")
80+
return ""
7381
}

routers/web/repo/blame.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
235235

236236
var lexerName string
237237

238+
avatarUtils := templates.NewAvatarUtils(ctx)
238239
i := 0
239240
commitCnt := 0
240241
for _, part := range blameParts {
@@ -257,9 +258,9 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
257258

258259
var avatar string
259260
if commit.User != nil {
260-
avatar = string(templates.Avatar(ctx, commit.User, 18, "gt-mr-3"))
261+
avatar = string(avatarUtils.Avatar(commit.User, 18, "gt-mr-3"))
261262
} else {
262-
avatar = string(templates.AvatarByEmail(ctx, commit.Author.Email, commit.Author.Name, 18, "gt-mr-3"))
263+
avatar = string(avatarUtils.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "gt-mr-3"))
263264
}
264265

265266
br.Avatar = gotemplate.HTML(avatar)

templates/base/head_navbar.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
{{if and .IsSigned .MustChangePassword}}
5858
<div class="ui dropdown jump item" data-tooltip-content="{{.locale.Tr "user_profile_and_more"}}">
5959
<span class="text gt-df gt-ac">
60-
{{avatar $.Context .SignedUser 24 "gt-mr-2"}}
60+
{{ctx.AvatarUtils.Avatar .SignedUser 24 "gt-mr-2"}}
6161
<span class="mobile-only gt-ml-3">{{.SignedUser.Name}}</span>
6262
<span class="not-mobile">{{svg "octicon-triangle-down"}}</span>
6363
</span>
@@ -143,7 +143,7 @@
143143

144144
<div class="ui dropdown jump item gt-mx-0 gt-pr-3" data-tooltip-content="{{.locale.Tr "user_profile_and_more"}}">
145145
<span class="text gt-df gt-ac">
146-
{{avatar $.Context .SignedUser 24 "gt-mr-2"}}
146+
{{ctx.AvatarUtils.Avatar .SignedUser 24 "gt-mr-2"}}
147147
<span class="mobile-only gt-ml-3">{{.SignedUser.Name}}</span>
148148
<span class="not-mobile">{{svg "octicon-triangle-down"}}</span>
149149
</span>

templates/explore/users.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{range .Users}}
99
<div class="flex-item flex-item-center">
1010
<div class="flex-item-leading">
11-
{{avatar $.Context . 48}}
11+
{{ctx.AvatarUtils.Avatar . 48}}
1212
</div>
1313
<div class="flex-item-main">
1414
<div class="flex-item-title">

templates/org/header.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="ui vertically grid head">
44
<div class="column">
55
<div class="ui header">
6-
{{avatar $.Context . 100}}
6+
{{ctx.AvatarUtils.Avatar . 100}}
77
<span class="text thin grey"><a href="{{.HomeLink}}">{{.DisplayName}}</a></span>
88
<span class="org-visibility">
99
{{if .Visibility.IsLimited}}<div class="ui medium basic horizontal label">{{$.locale.Tr "org.settings.visibility.limited_shortname"}}</div>{{end}}

templates/org/home.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{template "base/head" .}}
22
<div role="main" aria-label="{{.Title}}" class="page-content organization profile">
33
<div class="ui container gt-df">
4-
{{avatar $.Context .Org 140 "org-avatar"}}
4+
{{ctx.AvatarUtils.Avatar .Org 140 "org-avatar"}}
55
<div id="org-info">
66
<div class="ui header gt-df gt-fw">
77
{{.Org.DisplayName}}
@@ -61,7 +61,7 @@
6161
{{$isMember := .IsOrganizationMember}}
6262
{{range .Members}}
6363
{{if or $isMember (call $.IsPublicMember .ID)}}
64-
<a href="{{.HomeLink}}" title="{{.Name}}{{if .FullName}} ({{.FullName}}){{end}}">{{avatar $.Context . 48}}</a>
64+
<a href="{{.HomeLink}}" title="{{.Name}}{{if .FullName}} ({{.FullName}}){{end}}">{{ctx.AvatarUtils.Avatar . 48}}</a>
6565
{{end}}
6666
{{end}}
6767
</div>

templates/org/member/members.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{{$isPublic := index $.MembersIsPublicMember .ID}}
1010
<div class="flex-item {{if $.PublicOnly}}flex-item-center{{end}}">
1111
<div class="flex-item-leading">
12-
<a href="{{.HomeLink}}">{{avatar $.Context . 48}}</a>
12+
<a href="{{.HomeLink}}">{{ctx.AvatarUtils.Avatar . 48}}</a>
1313
</div>
1414
<div class="flex-item-main">
1515
<div class="flex-item-title">

templates/org/team/invite.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{template "base/alert" .}}
55
<div class="ui centered card">
66
<div class="image">
7-
{{avatar $.Context .Organization 140}}
7+
{{ctx.AvatarUtils.Avatar .Organization 140}}
88
</div>
99
<div class="content">
1010
<div class="header">{{.locale.Tr "org.teams.invite.title" .Team.Name .Organization.Name | Str2html}}</div>

0 commit comments

Comments
 (0)